[JAVA] count plus and minus / T : O(N), S : O(1)


  • 0
    J
    class Solution {
        public boolean isAnagram(String s, String t) {
            int[] alpha = new int[26];
            for(char ch : s.toCharArray()){
                alpha[ch - 'a']++;
            }
            
            for(char ch : t.toCharArray()){
                alpha[ch - 'a']--;
                if(alpha[ch - 'a'] < 0)
                    return false;
            }
            
            for(int count : alpha){
                if(count != 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.