Given two strings s and t, return true if t is an anagram of s, and false otherwise.
An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.
Example 1: Input: s = "anagram", t = "nagaram" Output: true
Example 2: Input: s = "rat", t = "car" Output: false
class Solution {
public boolean isAnagram(String s, String t) {
char[] s_array = s.toCharArray();
char[] t_array = t.toCharArray();
Arrays.sort(s_array);
Arrays.sort(t_array);
if (s_array.length == t_array.length) {
for (int i = 0; i <= s_array.length-1; i++) {
if (s_array[i] == t_array[i]) {
return true;
}
}
}
return false;
}
}
For Example 2, I'm getting the output as true when the expected is false.
I believe that my code should meet the requirements of the problem, but it is not working as expected. My approach involves converting the given string into a char array, sorting it, and then comparing it with another sorted char array. I expect that if the two arrays have the same values for each index, they are anagrams. However, it seems that there is something missing or incorrect in my approach.