您的位置:首页 > 其它

Cracking the Coding Interview Q1.3

2014-07-03 09:34 211 查看
Write a method to decide if two strings are anagrams or not.

public static boolean permutation(String s, String t) {
if (s.length() != t.length()) {
return false;
}

int[] letters = new int[256];

char[] s_array = s.toCharArray();
for (char c : s_array) { // count number of each char in s.
letters[c]++;
}

for (int i = 0; i < t.length(); i++) {
int c = (int) t.charAt(i);
if (--letters[c] < 0) {
return false;
}
}

return true;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: