Java AC - The most basic solution


  • 0
    A
    public class Solution {
        public boolean isAnagram(String s, String t) {
            if(s.length() != t.length()) return false;
            int ALL_CHARS = 255;
            int[] chars = new int[ALL_CHARS];
            for(int i = 0; i < s.length(); ++i) {
                chars[s.charAt(i)]++;
                chars[t.charAt(i)]--;
            }
            for(int i = 0; i < ALL_CHARS; ++i) {
                if(chars[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.