Share my java solution


  • 35
    A
        public class Solution {
        public boolean isAnagram(String s, String t) {
            if(s.length()!=t.length()){
                return false;
            }
            int[] count = new int[26];
            for(int i=0;i<s.length();i++){
                count[s.charAt(i)-'a']++;
                count[t.charAt(i)-'a']--;
            }
            for(int i:count){
                if(i!=0){
                    return false;
                }
            }
            return true;
        }
    }

  • 0
    public boolean isAnagram(String s, String t) {
        if(s.length()!=t.length()) return false;
        int anagram[] = new int[26], anagramlength = s.length(),index = -1;
        for(char c: s.toCharArray()) anagram[c-'a']++;
        while(++index<t.length()){
            if(anagram[t.charAt(index)-'a']-->0) anagramlength--;
        }
        return anagramlength==0;
    }

Log in to reply
 

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