Leetcode #242 Valid Anagram

Given two strings s and t, write a function to determine if t is an anagram of s.

For example,
s = "anagram", t = "nagaram", return true.
s = "rat", t = "car", return false.

Note:
You may assume the string contains only lowercase alphabets.


bool isAnagram(char* s, char* t) {
	int sLen = strlen(s);
	int tLen = strlen(t);
	int alphabet[26] = {0};
	int i, index;
	if(sLen != tLen) return false;
	for(i = 0; i < sLen; ++ i)
	{
		index = s[i] - 'a';
		++ alphbet[index];
	}
	for(i = 0; i < tLen; ++ i)
	{
		index = t[i] - 'a';
		if(alphbet[index] > 0)
			-- alphbet[index];
		else return false;
	}
	return true;
}

只考慮26的字母,直接暴力開一個26的數組。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章