Share my Java Solution


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

Log in to reply
 

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