[leetcod] Valid Anagram 判断颠倒字母顺序而构成的单词
2015-08-13 20:32
357 查看
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.
题目大意:判断两个字符串是否仅仅是字母的顺序有颠倒(只含小写单词)
bool isAnagram(char* s, char* t) {
int s_char_count[26] = {0};
int t_char_count[26] = {0};
int i = 0;
for(i = 0; s[i] != '\0'; i++)
{
s_char_count[s[i]-'a']++;
}
for(i = 0; t[i] != '\0'; i++)
{
t_char_count[t[i]-'a']++;
}
for(i = 0; i < 26; i++)
{
if(s_char_count[i] != t_char_count[i])
return false;
}
return true;
}运行时间0ms
For example,
s = "anagram", t = "nagaram", return true.
s = "rat", t = "car", return false.
Note:
You may assume the string contains only lowercase alphabets.
题目大意:判断两个字符串是否仅仅是字母的顺序有颠倒(只含小写单词)
bool isAnagram(char* s, char* t) {
int s_char_count[26] = {0};
int t_char_count[26] = {0};
int i = 0;
for(i = 0; s[i] != '\0'; i++)
{
s_char_count[s[i]-'a']++;
}
for(i = 0; t[i] != '\0'; i++)
{
t_char_count[t[i]-'a']++;
}
for(i = 0; i < 26; i++)
{
if(s_char_count[i] != t_char_count[i])
return false;
}
return true;
}运行时间0ms
相关文章推荐
- Linux C函数参考手册(PDF版)
- Lua教程(十七):C API简介
- C#实现打造气泡屏幕保护效果
- C/C++数据对齐详细解析
- 利用C语言来求最大连续子序列乘积的方法
- 字符串的组合算法问题的C语言实现攻略
- C 语言基础教程(我的C之旅开始了)[三]
- C++中的extern “C”用法详解
- C 语言基础教程(我的C之旅开始了)[七]
- 最大子矩阵问题实例解析
- C字符串操作函数实现方法小结
- 字典树的基本知识及使用C语言的相关实现
- C/C++ 宏详细解析
- C/C++实现对STORM运行信息查看及控制的方法
- 用C#生成不重复的随机数的代码
- 在c和c++中实现函数回调
- 浅析C/C++中被人误解的SIZEOF
- C 语言基础教程(我的C之旅开始了)[六]
- C/C++实现的游戏角色名称名字随机生成代码