Java O(n) 3ms Solution


  • 0
            public boolean isAnagram(String s, String t) {
    		if (s == null || t == null) {
    			return s == t;
    		}
    		if (s.equals(t)) {
    			return true;
    		}
    		return Arrays.equals(countChars(s), countChars(t));
    	}
    
    	public int[] countChars(String s) {
    		int[] count = new int[26];
    		int length = s.length();
    		char[] chars=s.toCharArray();
    		for (int i = 0; i < length; i++) {
    			count[chars[i] - 0x61]++;
    		}
    		return count;
    	}
    

Log in to reply
 

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