Cracking The Coding Interview 1.4
2014-04-01 16:47
344 查看
//Write a method to decide if two strings are anagrams or not. // // 变位词(anagrams)指的是组成两个单词的字符相同,但位置不同的单词。比如说, abbcd和abcdb就是一对变位词。 // // 使用一个固定数组大小记录各个字符出现的次数,同1.1 #include <iostream> using namespace std; bool isAnagrams(const char *a, const char *b) { int asize = strlen(a); int bsize = strlen(b); if (asize != bsize) { return false; } int isAna[26] = {0}; for (int i =0; i <asize; i++) { int t = a[i] - 'a'; isAna[t]++; t = b[i] - 'a'; isAna[t]--; } bool isA = true; for ( int j=0; j <26; j++) { if (isAna[j]!=0) { isA = false; } } return isA; } int main() { char *s1 = "fuckyou"; char *s2 = "youfuck"; cout<<isAnagrams(s1,s2); return 0; }
相关文章推荐
- Cracking the Coding Interview Q1.4
- cracking the coding interview No1.4
- Cracking The Coding Interview 3rd -- 1.4
- 判断一个二叉树是否是平衡二叉树 Cracking the coding interview 4.1
- cracking the coding interview problem solution 1.8
- Cracking the Coding Interview 8.7
- Cracking the coding interview--Q9.1
- Cracking the Coding Interview: Trees and Graphs
- Cracking the coding interview--Q1.4
- Cracking the coding interview--Q1
- Cracking the coding interview--Q1.3
- Cracking the coding interview--Q8.7
- Cracking the coding interview Q1.1
- Cracking the coding interview--Q2.1
- Cracking the coding interview(中文版)
- [Cracking the Coding Interview] Chapter 3 - Stacks and Queues
- 《Cracking the Coding Interview》——第2章:链表——题目4
- 《Cracking the Coding Interview》——第5章:位操作——题目3
- Cracking the coding interview--Q12.7
- Cracking the Coding Interview Q2.7