Anonymous Anonymous - 1 month ago
413 0

No description

Java

Anagrams

public boolean isAnagram(String s1, String s2) {
    if (s1.length() != s2.length()) return false;

    int[] letters = new int[256];	

	for (int i = 0; i < s1.length(); i++) {
        char c1 = Character.toLowerCase(s1.charAt(i));
        char c2 = Character.toLowerCase(s2.charAt(i));
		
        letters[c1]++;
        letters[c2]--;
    }

    for (int i : letters) {
        if (i != 0) return false;
    }

    return true;
}