O(n) Java Solution


  • 1
    P
    public boolean isAnagram(String s, String t) {
            if(s.length() != t.length()) return false;
            
            int[] res1 = new int[26];
            int[] res2 = new int[26];
            
            for(int i = 0; i< s.length(); i++){
                char c = s.charAt(i);
                int index = c - 97;
                res1[index] += 1;
            }
            
            for(int i = 0; i< t.length(); i++){
                char c = t.charAt(i);
                int index = c - 97;
                res2[index] += 1;
            }
            
            for(int i = 0; i< 26; i++){
                if(res1[i] != res2[i]){
                    return false;
                }
            }
            return true;
        }

  • 0
    V

    what does it mean by " c - 97 " ?


  • 0
    O

    check ASCII code please


Log in to reply
 

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