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.

题目意思:

  给定两个字符串s和t,判断字符串t是否由字符串s经过调整位置得到。(注意不是回文串)

解题思路:

  记录每个字符串中字符出现的次数,如果字符串t是由字符串s经过调整位置得到,则这两个字符出现的次数是相等的。

源代码:

 class Solution {
public:
bool isAnagram(string s, string t) {
if(s.size()!=t.size()) return false;
map<char,int> cnt;
for(int i = ; i < s.size(); ++ i) cnt[s[i]]++;
for(int i = ; i < t.size(); ++ i) cnt[t[i]]--;
for(map<char,int>::iterator iter = cnt.begin(); iter!=cnt.end(); ++ iter){
if(iter->second!=)return false;
}
return true;
}
};

Leetcode Valid Anagram的更多相关文章

  1. [LeetCode] Valid Anagram 验证变位词

    Given two strings s and t, write a function to determine if t is an anagram of s. For example, s = & ...

  2. LeetCode——Valid Anagram

    Description: Given two strings s and t, write a function to determine if t is an anagram of s. For e ...

  3. LeetCode() Valid Anagram 有问题!!!

    为什么第一个通过,第二个不行呢? class Solution { public: bool isAnagram(string s, string t) { if(s.size() != t.size ...

  4. LeetCode Valid Anagram (简单题)

    题意: 给出两个字符串s和t,判断串t是否为s打乱后的串. 思路: 如果返回的是true,则两个串的长度必定相等,所有字符出现的次数一样.那么可以统计26个字母的次数来解决,复杂度O(n).也可以排序 ...

  5. leetcode面试准备:Valid Anagram

    leetcode面试准备:Valid Anagram 1 题目 Given two strings s and t, write a function to determine if t is an ...

  6. LeetCode 242. 有效的字母异位词(Valid Anagram)

    242. 有效的字母异位词 LeetCode242. Valid Anagram 题目描述 给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的一个字母异位词. 示例 1: 输入: s ...

  7. 22. leetcode 242. Valid Anagram(由颠倒字母顺序而构成的字)

    22. 242. Valid Anagram(由颠倒字母顺序而构成的字) Given two strings s and t, write a function to determine if t i ...

  8. 【LeetCode】242. Valid Anagram (2 solutions)

    Valid Anagram Given two strings s and t, write a function to determine if t is an anagram of s. For ...

  9. [leetcode]242. Valid Anagram验证变位词

    Given two strings s and t , write a function to determine if t is an anagram of s. Example 1: Input: ...

随机推荐

  1. 百度地图API 批量添加 带检索功能的信息窗口

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  2. IIS GZip

    优点:提高网页响应速度(静态压缩会占用一定的存储空间,但是速度快,而动态压缩不占用存储空间,但是占用CPU时间,而且压缩比不恒定.) 缺点:动态压缩会影响CPU性能. win7:设置: iis管理器- ...

  3. 二刷Cracking the Coding Interview(CC150第五版)

    第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...

  4. Erlang C1500K长连接推送服务-性能

    Whatsapp已经使用Erlang在生产环境跑到96GB内存单机 3M长连接,参加:WhatsApp的Erlang世界.毕竟业务级别能达到Whatsapp那样极少,现在只有千万级,单机太多挂一台影响 ...

  5. rocketmq查看命令

    首先进入 RocketMQ 工程,进入/RocketMQ/bin   在该目录下有个 mqadmin 脚本 .  查看帮助:   在 mqadmin 下可以查看有哪些命令    a: 查看具体命令的使 ...

  6. cocos2d-x for android配置 & 运行 Sample on Linux OS

    1.从http://www.cocos2d-x.org/download下载稳定版 比如cocos2d-x-2.2 2.解压cocos2d-x-2.2.zip,比如本文将其解压到 /opt 目录下 3 ...

  7. 考前预习(Ubuntu配备)

    这几天考前预习,趁现在不想预习,写点之前就想写的东西吧. 贴一下个人认为有用的,在Ubuntu装机后的一些小事.不过挺杂的,主要是拿来给以后的自己看,以及让现在无聊的我有点事做. 首先,Ubuntu官 ...

  8. iOS企业分发证书制作

    自签名证书制作流程 打开终端,输入 openssl genrsa - ,生成名称为ca的秘钥 注:openssl生成的文件皆放在用户文档下(finder菜单栏'前往' - 电脑 -Macintosh ...

  9. svn上传工程之后下载,打开下载之后的工程缺少文件

    当我们把iOS的工程上传到SVN中,当我们再从SVN中下载下来,就会出现错误,这是什么原因呢?我这里出现的错误是找不到文件,后来知道原来是被屏蔽掉了,就是上传的时候不上传某个类型的文件.例如我出错就是 ...

  10. JS身份证号码校验

    var Wi = [ 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1 ]; // 加权因子 var ValideCode = [ 1, 0 ...