Java 2 loops solution


  • 0
    Y
        public boolean isAnagram(String s, String t) {
            
            if(s==null || t==null || s.length()!=t.length())
                return false;
            int[] n = new int[26];
            for(int i=0; i<s.length(); i++){
                n[s.charAt(i)-'a']++;
                n[t.charAt(i)-'a']--;
            }
            for(int i=0; i<n.length; i++)
                if(n[i] != 0)
                    return false;
            return true;
        }
    

Log in to reply
 

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