判断两个字符串是不是互为anagrams
2016-04-12 10:07
288 查看
Description
Write a function that checks whether two words are anagrams. Two words are anagrams if they contain the same letters in any order. For example, “silent” and “listen” are anagrams. The header of the function is as follows:
bool isAnagram(const char * const s1, const char * const s2)
代码实现:
Write a function that checks whether two words are anagrams. Two words are anagrams if they contain the same letters in any order. For example, “silent” and “listen” are anagrams. The header of the function is as follows:
bool isAnagram(const char * const s1, const char * const s2)
代码实现:
#include<iostream> #include<string.h> using namespace std; bool isAnagram(const char * const s1, const char * const s2) { if(s1 == NULL || s2 == NULL){ return false; } int len1 = strlen(s1); int len2 = strlen(s2); if(len1 != len2) return false; int flag = true; //使用哈希表来判断,因为只有字符串中最多只有128个不同的字符 //所以可以建立一个array[128]的哈希表 int array[128] = {0}; for(int i = 0; i < len1; i++){ array[*(s1+i)]++; } for(int i = 0; i < len1; i++){ array[*(s2+i)]--; } for(int i = 0; i < 128; i++){ if(array[i] != 0){ flag = false; break; } } return flag; }
相关文章推荐
- NGUI在5.3打包失败问题
- 同班同学20年后 身家15亿与月薪5000元的区别
- 欢迎访问我的个人博客:www.aidandai.com
- ecshop前后台函数整理
- NGUI在5.3打包失败问题
- mac os x下的软件包管理神器---homebrew
- C#开发微信门户及应用(31)--微信语义理解接口的实现和处理
- Windows8 64安装Oracle 11g说明
- myeclipse 安装SVN插件
- /etc/passwd与/etc/shadow配置详解
- 2016年全球人工智能技术大会的“根”(Root)源自何处?
- python简史
- linux awk命令
- Openstack学习笔记(十)-Keystone学习笔记(一)
- Android Studio 显示行号
- 从此不求人:自主研发一套PHP前端开发框架(20)
- 关于数据:先加载还是先展示?
- 数据库” 查询选修所有课的学生信息“ exists解法的理解
- CastleWindsor 使用说明
- Android 返回键的处理