Last active
February 25, 2019 00:55
-
-
Save qiaoxu123/57e1f253bcd97102b1cedb51245f7327 to your computer and use it in GitHub Desktop.
> 很简单的字符串查找比较题目 - Approach 1:Brute Force
> 暴力求解,将字符串读取到数组中使用,然后挨个进行对比 - Approach 2 : set
> 使用set数组进行字符的查找,然后判断是否相同。相比方法一,性能有一些提升 - Approach 3:array
> 由于题目声明只是用小写字母,因此可以使用array代替set实现 - Approach 4:sort
> 使用sort对两个字符串进行排序,思路简单但是实现很耗时
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Runtime: 12 ms, faster than 97.85% | |
//Memory Usage: 9.2 MB, less than 58.97% | |
class Solution { | |
public: | |
bool isAnagram(string s, string t) { | |
if(s.length() != t.length()) return false; | |
int counts[26] = {0}; | |
for(int i = 0;i < s.length();++i){ | |
counts[s[i] - 'a']++; | |
counts[t[i] - 'a']--; | |
} | |
for(int i = 0;i < 26;++i) | |
if(counts[i]) return false; | |
return true; | |
} | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Runtime: 28 ms, faster than 25.25% | |
//Memory Usage: 9.8 MB, less than 5.16% | |
class Solution { | |
public: | |
bool isAnagram(string s, string t) { | |
if(s.length() != t.length()) return false; | |
vector<char> array1,array2; | |
for(int i = 0;i < s.length();++i){ | |
array1.push_back(s[i]); | |
array2.push_back(t[i]); | |
} | |
sort(array1.begin(),array1.end()); | |
sort(array2.begin(),array2.end()); | |
for(int i = 0;i < s.length();++i) | |
if(array1[i] != array2[i]) | |
return false; | |
return true; | |
} | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Runtime: 16 ms, faster than 58.39% | |
//Memory Usage: 9.3 MB, less than 15.23% | |
class Solution { | |
public: | |
bool isAnagram(string s, string t) { | |
if(s.length() != t.length()) return false; | |
unordered_map<char,int> set1,set2; | |
for(int i = 0;i < s.length();++i){ | |
set1[s[i]]++,set2[t[i]]++; | |
} | |
for(int i = 0;i < s.length();++i) | |
if(set1[s[i]] != set2[s[i]]) | |
return false; | |
return true; | |
} | |
}; | |
//进一步优化 | |
class Solution { | |
public: | |
bool isAnagram(string s, string t) { | |
if (s.length() != t.length()) return false; | |
int n = s.length(); | |
unordered_map<char, int> counts; | |
for (int i = 0; i < n; i++) { | |
counts[s[i]]++; | |
counts[t[i]]--; | |
} | |
for (auto count : counts) | |
if (count.second) return false; | |
return true; | |
} | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Runtime: 28 ms, faster than 25.25% | |
//Memory Usage: 9.2 MB, less than 58.97% | |
class Solution { | |
public: | |
bool isAnagram(string s, string t) { | |
sort(s.begin(), s.end()); | |
sort(t.begin(), t.end()); | |
return s == t; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment