Leetcode Valid Anagram
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的更多相关文章
- [LeetCode] Valid Anagram 验证变位词
Given two strings s and t, write a function to determine if t is an anagram of s. For example, s = & ...
- LeetCode——Valid Anagram
Description: Given two strings s and t, write a function to determine if t is an anagram of s. For e ...
- LeetCode() Valid Anagram 有问题!!!
为什么第一个通过,第二个不行呢? class Solution { public: bool isAnagram(string s, string t) { if(s.size() != t.size ...
- LeetCode Valid Anagram (简单题)
题意: 给出两个字符串s和t,判断串t是否为s打乱后的串. 思路: 如果返回的是true,则两个串的长度必定相等,所有字符出现的次数一样.那么可以统计26个字母的次数来解决,复杂度O(n).也可以排序 ...
- leetcode面试准备:Valid Anagram
leetcode面试准备:Valid Anagram 1 题目 Given two strings s and t, write a function to determine if t is an ...
- LeetCode 242. 有效的字母异位词(Valid Anagram)
242. 有效的字母异位词 LeetCode242. Valid Anagram 题目描述 给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的一个字母异位词. 示例 1: 输入: s ...
- 22. leetcode 242. Valid Anagram(由颠倒字母顺序而构成的字)
22. 242. Valid Anagram(由颠倒字母顺序而构成的字) Given two strings s and t, write a function to determine if t i ...
- 【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 ...
- [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: ...
随机推荐
- (转)C#根据当前时间获取周,月,季度,年度等时间段的起止时间
DateTime dt = DateTime.Now; //当前时间 DateTime startWeek = dt.AddDays( - Convert.ToInt32(dt.DayOfWeek.T ...
- 耿丹CS16-2班第七次作业汇总
Deadline: 2016-11-27 11:59pm 作业内容 第七次作业总结 01.每次成绩发布,麻烦没交作业的同学(暂定得分为-5的),请及时补交: 02.想不出来可以,代码乱成一团不行,命名 ...
- jquery validate minlength rule is not working
Question: I have a form with a password field. The password has to be at least 8 characters long. &l ...
- ASP.NET SignalR入门
前言 之前在培训ASP.NET WebAPI的时候有提过SignalR这个技术,但当时只是讲了是用来做什么的,并没有多说.因为自己也是画图找资料的时候见到的.后来当一直关注的前端大神贤心发布LayIM ...
- Git 常用操作和问题解决
记录一下自己用git作为项目管理过程中常见的错误以及处理方法 1.git pull 出现问题 git pull出现的问题多为远程分支文件和本地冲突 错误提示:error: Your local cha ...
- EF jsonignore
页面单独指定不循环引用 [JsonIgnore] Newtonsoft.Json.JsonSerializerSettings jsSettings = new Newtonsoft.Json.Jso ...
- sh1.shell脚本练习
练习: 1.写一个脚本 判断当前系统上是否有用户的默认shell为bash 如果有,就显示其中一个的用户名:否则,就显示没有这类用户. #!/bin/bash grep "bash$&quo ...
- swift 的枚举、结构体、类
一.Swift的枚举 枚举是一系相关联的值定义的一个公共的组类型,同时能够让你在编程的时候在类型安全的情况下去使用这些值.Swift中的枚举比OC中的枚举强大得多, 因为Swift中的枚举是一等类型, ...
- windows下webstorm开发react-native智能提示
webstorm破解版地址:点这里 1.随便在一个目录下下载ReactNative-LiveTemplate插件,命令为: git clone https://github.com/virtoolsw ...
- win10下安装mysql5.7.16(解压缩版)
注:本文涉及的是解压缩版的安装 安装教程 下载mysql 地址是:http://dev.mysql.com/downloads/mysql/ 解压缩下载的文件 修改ini文件(在解压缩后的mysql文 ...