(String s, String t)
| 71 | // 3. Efficient approach using hashing |
| 72 | // Time: O(n) | Space: O(1) [because the table's size stays constant no matter how large n is] |
| 73 | public boolean isAnagram(String s, String t) { |
| 74 | |
| 75 | int [] charCount = new int[128]; |
| 76 | |
| 77 | for (int i=0; i<s.length(); i++){ |
| 78 | charCount[s.charAt(i)]++; |
| 79 | } |
| 80 | |
| 81 | for (int i=0; i<t.length(); i++){ |
| 82 | charCount[t.charAt(i)]--; |
| 83 | } |
| 84 | |
| 85 | for (int i=0;i<128;i++){ |
| 86 | if(charCount[i] != 0){ |
| 87 | return false; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | return true; |
| 92 | } |
| 93 | } |