cracking the coding interview No1.3
2014-10-19 15:26
344 查看
1.3 Given two strings,write a method to decide if one is permutation of the other.
//(1)O(nlogn)排序后比较字符串 //(2)O(n)hash table bool isAnagram (string s1, string s2) { if (s1 == NULL || s2 == NULL) { return false; } if (s1.length() != s2.length) { return false; } int length = s1.length(); int hash[256]; memset(hash,0,sizeof(hash)); for (int i = 0; i < length; i++) { hash[(int)s1[i]]++; hash[(int)s2[i]]--; } for (int i = 0; i < 256; i++) { if (hash[i]) { return false; } } return true; }
相关文章推荐
- Cracking the Coding Interview Q1.3
- cracking the coding interview No1.6
- cracking the coding interview No1.7
- cracking the coding interview No1.1
- cracking the coding interview No1.2
- cracking the coding interview No1.8
- cracking the coding interview problem solution 1.3
- Cracking The Coding Interview 3rd -- 1.3
- Cracking The Coding Interview 1.3
- cracking the coding interview No1.4
- cracking the coding interview No1.5
- Cracking the coding interview--Q9.6
- Cracking the Coding Interview 5.2
- Cracking the coding interview--Q12.3
- 《Cracking the Coding Interview》——第1章:数组和字符串——题目7
- 《Cracking the Coding Interview》——第4章:树和图——题目3
- 《Cracking the Coding Interview》——第6章:智力题——题目1
- 《Cracking the Coding Interview》——第6章:智力题——题目6
- 《Cracking the Coding Interview》——第9章:递归和动态规划——题目9
- Cracking the coding interview--Q9.3