# Very easy to follow solution[Java]

• ``````public class Solution {
public boolean isAnagram(String s, String t) {

char[] charArr = s.toCharArray(); //Convert both strings to arrays
Arrays.sort(charArr);             //Sort the arrays alphabetically
String result = new String(charArr); //Create new strings to hold the values of the sorted arrays

char[] charArr2 = t.toCharArray();
Arrays.sort(charArr2);
String result2 = new String(charArr2);

return result.equals(result2); //Compare the two strings, if they are equal, they are anagrams of eachother
}
}``````

• the complexity of your algorithm is O(n log n), where n is length of strings,

but if count number of each letter in strings and then compare numbers of corresponding letter complaxity
will be O(n + n + 256) = O(n)

`````` class Solution {
public:
bool isAnagram(string s, string t) {
int letterCntS[333], letterCntT[333];
if (s.size() != t.size()) return false;
memset(letterCntS, 0, sizeof(letterCntS));
memset(letterCntT, 0, sizeof(letterCntT));
for (int i = 0; i < s.size(); i++) {
letterCntS[s[i]]++;
letterCntT[t[i]]++;
}
for (int i = 0; i < 300; i++)
if (letterCntS[i] != letterCntT[i]) return false;
return true;
}
};``````

• It says:"You may assume the string contains only lowercase alphabets.", so you just need a
int array of 26 .
my accepted java solution:

public boolean isAnagram(String s, String t) {

``````	if (s.length() == t.length() ) {

int[] map1 = new int[26];
int[] map2 = new int[26];

for (int i = 0; i < s.length(); i++) {
map1[s.charAt(i)-'a']++;
}
for (int i = 0; i < t.length(); i++) {
map2[t.charAt(i)-'a']++;
}
for (int i = 0; i < 26; i++) {
if (map1[i] != map2[i]) {
return false;
}
}

return true;
} else {
return false;
}
}
``````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.