Simple O(n) Java solution


  • 0
    R
    public boolean isAnagram(String s, String t) {
            if(s.length() != t.length())
                return false;
            int [] arr = new int[26];
            for(int i=0;i<s.length();i++) {
                arr[s.charAt(i)-'a'] += 1;
                arr[t.charAt(i)-'a'] -= 1;
            }
            for(int i=0;i<26;i++){
                if(arr[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.