242. Valid Anagram
2016-06-02 09:57
302 查看
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.
Follow up:
What if the inputs contain unicode characters? How would you adapt your solution to such case?
使用set集合排序 Runtime: 236
ms
class Solution {
public:
bool isAnagram(string s, string t) {
multiset<char> set1;
multiset<char> set2;
for(auto it=s.begin();it!=s.end();it++)
set1.insert(*it);
for(auto it=t.begin();it!=t.end();it++)
set2.insert(*it);
if(set1!=set2)
return false;
return true;
}
};
使用sort算法 Runtime: 80
ms
class Solution {
public:
bool isAnagram(string s, string t) {
sort(s.begin(),s.end());
sort(t.begin(),t.end());
return s==t;
}
};
使用hash table unordered_map Runtime: 36
ms
class Solution {
public:
bool isAnagram(string s, string t) {
if(s.size()!=t.size()) return false;
int len=s.size();
unordered_map<char,int> counts;
for(int i=0;i<len;i++)
{
counts[s[i]]++;
counts[t[i]]--;
}
for(auto count:counts)
if(count.second) return false;
return true;
}
};
使用数组代替unordered_map获得更短的时间 Runtime: 12
ms
class Solution {
public:
bool isAnagram(string s, string t) {
if(s.size()!=t.size()) return false;
int len=s.size();
int counts[26]={0};
for(int i=0;i<len;i++)
{
counts[s[i]-'a']++;
counts[t[i]-'a']--;
}
for(int i=0;i<26;i++)
if(counts[i]) return false;
return true;
}
};
For example,
s = "anagram", t = "nagaram", return true.
s = "rat", t = "car", return false.
Note:
You may assume the string contains only lowercase alphabets.
Follow up:
What if the inputs contain unicode characters? How would you adapt your solution to such case?
使用set集合排序 Runtime: 236
ms
class Solution {
public:
bool isAnagram(string s, string t) {
multiset<char> set1;
multiset<char> set2;
for(auto it=s.begin();it!=s.end();it++)
set1.insert(*it);
for(auto it=t.begin();it!=t.end();it++)
set2.insert(*it);
if(set1!=set2)
return false;
return true;
}
};
使用sort算法 Runtime: 80
ms
class Solution {
public:
bool isAnagram(string s, string t) {
sort(s.begin(),s.end());
sort(t.begin(),t.end());
return s==t;
}
};
使用hash table unordered_map Runtime: 36
ms
class Solution {
public:
bool isAnagram(string s, string t) {
if(s.size()!=t.size()) return false;
int len=s.size();
unordered_map<char,int> counts;
for(int i=0;i<len;i++)
{
counts[s[i]]++;
counts[t[i]]--;
}
for(auto count:counts)
if(count.second) return false;
return true;
}
};
使用数组代替unordered_map获得更短的时间 Runtime: 12
ms
class Solution {
public:
bool isAnagram(string s, string t) {
if(s.size()!=t.size()) return false;
int len=s.size();
int counts[26]={0};
for(int i=0;i<len;i++)
{
counts[s[i]-'a']++;
counts[t[i]-'a']--;
}
for(int i=0;i<26;i++)
if(counts[i]) return false;
return true;
}
};
相关文章推荐
- VFP Control - When | Valid
- extjs form model vilidations
- The server xxx.xxx.xxx.xxx failed to respond wi...
- Valid Anagram
- djongo form.is_valid 返回false的解决方法
- cci-Q1.4 判断两个字符串是否为变位词
- Anagrams
- Validate Binary Search Tree
- Longest Valid Parentheses
- SpringMVC 使用JSR-303进行校验 @Valid
- form提交时,哪些数据是有效的
- Leetcode -- Valid Sudoku
- Lintcode(1)-两个字符串是变位词
- Valid Parentheses
- 简单的leetcode(二)
- LeetCode:Valid Parentheses算法详解
- [LeetCode]36. Valid Sudoku
- JSF Value is not valid异常
- leetcode笔记:Anagrams
- Jenkins出现No valid crumb was included in the reques