Lintcode--002(两个字符串是变位词)
写出一个函数 anagram(s, t) 判断两个字符串是否可以通过改变字母的顺序变成一样的字符串。
给出 s = "abcd",t="dcab",返回 true.
给出 s = "ab", t = "ab", 返回 true.
给出 s = "ab", t = "ac", 返回 false.
class Solution {
public:
/**
* @param s: The first string
* @param b: The second string
* @return true or false
*/
bool anagram(string s, string t) {
// write your code here
//Lintcode--002;
//此题目应该与Lintcode--001属于同类型题目,核心思路一致,只是最后两个字符串做的比较不一样;
//这个题目甚至更简单一些,但是细节上需要注意ASCII码表,一些常见字符对应的十进制值,这有利于以后使用;
//解析可参考Lintcode--001;
//边界条件,长度如果不相等,直接pass;
if(s.size()!=t.size()){
return false;
}
//主要任务应该是比较两个字符串,各个字母出现的次数是否相同;
//创建数组,用来统计各种字符出现的次数;
else {
int letters1[];
memset(letters1,,sizeof(letters1));
for(int i=;i<s.size();i++){
letters1[s[i]-' ']++;
}
int letters2[];
memset(letters2,,sizeof(letters2));
for(int j=;j<t.size();j++){
letters2[t[j]-' ']++;
}
for (int k=;k<;){
if (letters1[k]==letters2[k]){
k=k+;
}
else
{
return false;
}
}
return true;
}
}
};
附:ASCII码表

Lintcode--002(两个字符串是变位词)的更多相关文章
- lintcode-158-两个字符串是变位词
158-两个字符串是变位词 写出一个函数 anagram(s, t) 判断两个字符串是否可以通过改变字母的顺序变成一样的字符串. 说明 What is Anagram? Two strings are ...
- LintCode-两个字符串是变位词
题目描述: 写出一个函数 anagram(s, t) 去判断两个字符串是否是颠倒字母顺序构成的 样例 给出 s="abcd",t="dcab",返回 true ...
- 剑指Offer:互为变位词
// 判断两个单词是否互为变位词: 如果两个单词中的字母相同,并且每个字母出现的次数也相同, 那么这两个单词互为变位词 #include <stdio.h> #include <st ...
- [Swust 549]--变位词(vector水过)
Time limit(ms): 1000 Memory limit(kb): 65535 Description 输入N和一个要查找的字符串,以下有N个字符串,我们需要找出其中的所有待查找字符串的 ...
- [LeetCode] 49. Group Anagrams 分组变位词
Given an array of strings, group anagrams together. For example, given: ["eat", "tea& ...
- 005推断两个字符串是否是变位词 (keep it up)
写一个函数推断两个字符串是否是变位词. 变位词(anagrams)指的是组成两个单词的字符同样,但位置不同的单词.比方说, abbcd和abcdb就是一对变位词 这也是简单的题. 我们能够排序然后对照 ...
- [LeetCode] Find All Anagrams in a String 找出字符串中所有的变位词
Given a string s and a non-empty string p, find all the start indices of p's anagrams in s. Strings ...
- 【easy】438.Find All Anagrams in a String 找出字符串中所有的变位词
Input: s: "abab" p: "ab" Output: [0, 1, 2] Explanation: The substring with start ...
- [LeetCode] 438. Find All Anagrams in a String 找出字符串中所有的变位词
Given a string s and a non-empty string p, find all the start indices of p's anagrams in s. Strings ...
随机推荐
- VC++中调用cmd的集中方式
1. system方法: 原型: int __cdecl system(const char *); 例如: system("ipconfig"); 2. WinExec方法: 相 ...
- op编译信赖的库
Table of known prerequisites and their corresponding packages Here's a table with the package name f ...
- [又是BUG]常见的RuntimeException
妈蛋这异常那异常都是异常,不能忍了! 下面总结一些经常遇到的异常(RuntimeExecption): 算术异常类:ArithmeticExecption 数组下标越界异常:ArrayIndexO ...
- [转]10款 Web 开发常备工具
文章地址:https://my.oschina.net/u/2903254/blog/798135 工欲善其事,必先利其器.如今 Web 开发标准越来越高,Web 开发者也在不断寻找途径提升自己的技能 ...
- HDU-1428(记忆化搜索)
Problem Description LL 最近沉迷于AC不能自拔,每天寝室.机房两点一线.由于长时间坐在电脑边,缺乏运动.他决定充分利用每次从寝室到机房的时间,在校园里散散步.整个HDU 校园呈方 ...
- HDFS文件读取详解
客户端与HDFS文件读取 创建HDFS文件系统实例 FileSystem fs = FileSystem.get(new URI("hdfs://ns1"), new Config ...
- Apache-common项目提供的工具
---- MD5加密与生成UUID例子(依赖于commons-io.jar):begin ------------------------------------------------------- ...
- V$、GV$、X$、V_$、GV_$之间的关系
V$.GV$.X$.V_$.GV_$之间的关系 GV$:全局视图,针对多个实例环境. V$:针对某个实例的视图. X$:是GV$视图的数据来源,oracle内部表. GV_$:是GV$的同义词. V_ ...
- ios应用内跳转到appstore里评分
NSString *str = [NSString stringWithFormat:@"itms-apps://ax.itunes.apple.com/WebObjects/MZStore ...
- Linux Resin 安装配置
Resin是一个非常流行的application server,对servlet和JSP提供了良好的支持,性能优良,resin自身采用Java语言开发.Resin Pro版本支持缓存和负载均衡,收费最 ...