-2

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.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
  • 1
    Your current code returns `true` immediately on a character match. You need to check **all** characters. (But see previous comment.) – Dave Newton May 04 '23 at 18:10
  • Crap, I was thinking palindromes. Ignore original comment. I'm not sure which is worse--that I spaced out, or that it was still in my head 30 minutes later. – Dave Newton May 04 '23 at 18:40

3 Answers3

1

After sorting you only have to check if the arrays are equal and return that as the result:

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);

        return Arrays.equals(s_array,t_array);
    }
}

In your code you should only return false when they don't match:

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 false;
                }
            }
            return true;
        }
        return false;
    }
}
Bill Mair
  • 1,073
  • 6
  • 15
0

The problem is that you are short circuiting your character comparison on a match without checking the other characters and returning true prematurely. You need to short circuit on a mis-match and return false, otherwise return true after the for loop completes.

But no sense reinventing the wheel on comparisons. Just use Arrays.compare.

public static boolean isAnagram(String s, String t) {
     char[] s_array = s.toCharArray();
     char[] t_array = t.toCharArray();

     Arrays.sort(s_array);
     Arrays.sort(t_array);
     return Arrays.compare(s_array,t_array) == 0;
}
WJS
  • 36,363
  • 4
  • 24
  • 39
0

Refer to this line of your code:

for (int i = 0; i <= s_array.length-1; i++) {
    if (s_array[i] == t_array[i]) {
        return true;

so when rat and cat are arranged you get
act and art respectively so after comparison of the first character, i.e. 'a', you are returning true and not checking the remaining ones as you can see that on the comparison of second character you get false. This is your error.

Here is a solution to your problem:

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 false;
            }
        }
        return true;
    }
    return false;
}

I hope it helps.

Abra
  • 19,142
  • 7
  • 29
  • 41
Zaeem Ali
  • 1
  • 1