When I tried to take two strings as input to a function and check whether they are anagrams, I am getting the wrong output. I have written the following code.
class Solution {
public boolean isAnagram(String s, String t) {
char sArray[] = s.toCharArray();
char tArray[] = t.toCharArray();
Arrays.sort(sArray);
Arrays.sort(tArray);
s = sArray.toString();
t = tArray.toString();
return s.equals(t);
}
}
The sample input I have taken is s = "anagram" and t = "nagaram"
. When checked, both the char arrays are printing the same value, i.e.
sArray is:
aaagmnr
tArray is:
aaagmnr
But my output seems to be false. Could someone please help me why am I getting such result when I use equals()
after toString()
?