| title | lintcode-anagrams |
|---|---|
| tags | 新建,模板,小书匠 |
| grammar_cjkRuby | true |
lintcode Two Strings Are Anagrams
bool anagram(string s, string t) {
// write your code here
if (s.size() != t.size())
return false;
map<char, int> maps, mapt;
for (string::size_type ids = 0; ids < s.size(); ids++)
{
if (maps.find(s[ids]) == maps.end())
maps[s[ids]] = 1;
else
maps[s[ids]] ++;
}
for (string::size_type idt = 0; idt < s.size(); idt++)
{
if (mapt.find(t[idt]) == mapt.end())
mapt[t[idt]] = 1;
else
mapt[t[idt]] ++;
}
if (maps!=mapt)
return false;
return true;
}bool anagram(string s, string t) {
// write your code here
if (s.size() != t.size())
return false;
int charNum[256] = {0};//初始化一个全0的数组;
for(int i = 0;i < s.length();i++)
{
charNum[s[i]]++;
charNum[t[i]]--;
}
for(int i = 0;i < s.length();i++)
if(charNum[s[i]] != 0)
return false;
return true;
}bool anagram(string s, string t) {
// write your code here
sort(s.begin(),s.end());
sort(t.begin(),t.end());
if(s != t)
return false;
return true;
}