Valid Anagram

Leetcode 242.

Version 1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution {
public boolean isAnagram(String s, String t) {
int l1 = s.length();
int l2 = t.length();
if(l1 != l2)
return false;
int[] store = new int[26];
for(int i = 0, j = 0; i < l1 && j < l2;i++,j++){
store[s.charAt(i) - 'a']++;
store[t.charAt(j) - 'a']--;
}
for(int value: store){
if(value != 0)
return false;
}

return true;
}
}

Version 2

1
2
3
4
5
6
7
8
9
10
class Solution {
public boolean isAnagram(String s, String t) {
char[] array1 = s.toCharArray();
char[] array2 = t.toCharArray();
Arrays.sort(array1);
Arrays.sort(array2);
if(Arrays.equals(array1, array2)) return true;
return false;
}
}