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 ...
随机推荐
- day03_javaEE四成结构
- bzoj2741【FOTILE模拟赛】L
http://www.lydsy.com/JudgeOnline/problem.php?id=2741 分块或可持久化trie 可以先看看这个:高斯消元解XOR方程组 分块做法: 我们先求出前i个数 ...
- linux image writes boot log to console
- jquery中的属性和css
jQuery中的属性用于获取或设置元素的属性 1.attr(),获取或设置所有相匹配的元素的属性值:removeAttr("attr"),移除所有相匹配的元素的属性 //html ...
- 10个利用Eclipse调试Java的常见技巧
http://www.open-open.com/news/view/1ad9099 阅读目录 1. Conditional Breakpoint 2. Exception Breakpoint 3. ...
- openfire连接登陆优化方案
client登陆openfire,大概总共须要9个来回才完毕登录. 在2G情况下.就表现为client登录特别慢,所以,为解决问题,对openfire进行了例如以下优化 openfire的连接.登陆过 ...
- codeforces Arrival of the General 题解
A Ministry for Defense sent a general to inspect the Super Secret Military Squad under the command o ...
- 为什么HikariCP被号称为性能最好的Java数据库连接池,怎样配置使用
HiKariCP是数据库连接池的一个后起之秀.号称性能最好.能够完美地PK掉其它连接池. 原文地址:http://blog.csdn.net/clementad/article/details/469 ...
- license文件生成原理
byte解密weblogic加密oraclehex 现在很多J2EE应用都采用一个license文件来授权系统的使用,特别是在系统购买的早期,会提供有限制的license文件对系统进行限制,比如试 ...
- C语言函数指针(转载)
二.通常的函数调用 一个通常的函数调用的例子:/* 自行包含头文件 */void MyFun(int x); /* 此处的声明也可写成:void MyFun(int) */int main(int a ...