您的位置:首页 > 编程语言 > Go语言

LeetCode Algorithms #242 <Valid Anagram>

2016-01-31 17:10 281 查看
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.

思路:

没有一点难度的问题,貌似有个类似的题我用数组实现过了,所以这次用map实现。

解:

bool isAnagram(std::string s, std::string t) {
std::map<char, int> charNum_s;
std::map<char, int> charNum_t;

if(s.size() != t.size())
{
return false;
}

for(size_t strLength_s = 0; strLength_s < s.size(); strLength_s++)
{
charNum_s[s[strLength_s]]++;
}

for(size_t strLength_t = 0; strLength_t < s.size(); strLength_t++)
{
charNum_t[t[strLength_t]]++;
}

if(charNum_s.size() != charNum_t.size())
{
return false;
}

for(size_t strLength_s = 0; strLength_s < s.size(); strLength_s++)
{
if(charNum_s[s[strLength_s]] != charNum_t[s[strLength_s]])
{
return false;
}
}
return true;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: