# My solutions in C++, Java, Python, C, C#, JavaScript, and Ruby

• C++:

``````        int c[256] = {};
for (int i = 0; i < s.size(); i++) { c[s[i]]++; }
for (int i = 0; i < t.size(); i++) { c[t[i]]--; }
for (int i = 0; i < 256; i++) {
if (c[i]) { return false; }
}
return true;
``````

Java:

``````    int[] c = new int[256];
for (int i = 0; i < s.length(); i++) {
c[s.charAt(i)]++;
}
for (int i = 0; i < t.length(); i++) {
c[t.charAt(i)]--;
}
return Arrays.stream(c).reduce(0, (a, b) -> Math.abs(a) + Math.abs(b)) == 0;
``````

Python:

``````return sorted(s) == sorted(t)
``````

C:

``````bool isAnagram(char* s, char* t) {
int c[256];
for (int i = 0; i < 256; i++) { c[i] = 0; }
while (*s != '\0') { c[*s++]++; }
while (*t != '\0') { c[*t++]--; }
for (int i = 0; i < 256; i++) {
if (c[i] != 0) {
return false;
}
}
return true;
}
``````

C#:

``````    return new string(s.OrderBy(c => c).ToArray()) == new string(t.OrderBy(c => c).ToArray());
``````

JavaScript:

``````return s.split('').sort().toString() === t.split('').sort().toString()
``````

Ruby:

``return s.split('').sort() == t.split('').sort()``

• Accepted Simple easy to understand solution O(n)

``````public boolean isAnagram(String s, String t) {
if(s==null && t==null )
return true;
if((s==null && t!=null )  || (s!=null && t==null ))
return false;
if(s.length() != t.length())
return false;

char[] ss = s.toCharArray();
char[] tt = t.toCharArray();
Arrays.sort(ss);
Arrays.sort(tt);
return Arrays.equals(ss,tt);
}``````

• @xcv58 what is time and space complexity for your python solution?

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