题目:

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.

思路:

统计第一个字符串的每一个字符出现的次数,再统计第二个字符串的每一个字符出现的次数。然后比較,假设都同样则返回true。

假设不同返回false。

代码1:

class Solution {
public:
bool isAnagram(string s, string t)
{
int len1 = s.size();
int len2 = t.size();
if(len1 != len2)
return false;
int map[26] = {0};//因为题目确定每一个字符都是小写字母。因此仅仅须要申请26个空间就可以
for(int i = 0 ; i < len1 ; i++)
{
map[s[i]-'a']++;
}
for(int i = 0 ; i < len1 ; i++)
{
map[t[i]-'a']--;
if(map[t[i]-'a'] < 0)
return false;
}
return true;
}
};

代码2:

class Solution {
public:
bool isAnagram(string s, string t)
{
int len1 = s.size();
int len2 = t.size();
if(len1 != len2)
return false;
int map[26] = {0};//因为题目确定每一个字符都是小写字母,因此仅仅须要申请26个空间就可以
for(int i = 0 ; i < len1 ; i++)
{
map[s[i]-'a']++;
map[t[i]-'a']--;
}
for(int i = 0 ; i < len1 ; i++)
{
if(map[t[i]-'a'] != 0)
return false;
}
return true;
}
};

LeetCode OJ 之 Valid Anagram的更多相关文章

  1. LeetCode OJ:Valid Anagram(有效字谜问题)

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

  2. leetcode面试准备:Valid Anagram

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

  3. 【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 ...

  4. 【LeetCode OJ】Valid Palindrome

    Problem Link: http://oj.leetcode.com/problems/valid-palindrome/ The following two conditions would s ...

  5. 【一天一道LeetCode】#242. Valid Anagram

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given t ...

  6. LeetCode算法题-Valid Anagram(Java实现)

    这是悦乐书的第198次更新,第205篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第61题(顺位题号是242).给定两个字符串s和t,写一个函数来确定t是否是s的anag ...

  7. LeetCode OJ——Longest Valid Parentheses

    http://oj.leetcode.com/problems/longest-valid-parentheses/ 最大括号匹配长度,括号是可以嵌套的 #include <string> ...

  8. 【LeetCode】242. Valid Anagram 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 字典统计词频 排序 日期 [LeetCode] 题目地址:ht ...

  9. 【LeetCode】242 - Valid Anagram

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

随机推荐

  1. ACM_水题你要信了(修改版)

    水题你要信了 Time Limit: 2000/1000ms (Java/Others) Problem Description: 某发最近又认识了很多妹(han)子,可是妹(han)子一多不免有时会 ...

  2. Spring Cloud (7) 服务容错保护-Hystrix服务降级

    在微服务架构中,根据业务来拆分成一个个的服务,服务与服务之间可以互相调用,在Spring Cloud可以用RestTemplate+Ribbon和Feign来调用.为了保证其高可用,单个服务通常会集群 ...

  3. 更新换代之requests库

    好久不更新博客了... 之前的博文都是通过urllib2进行http访问,接下来我要说一个利器啊!requests模块,无法用语言对他进行赞扬了,需要的,有兴趣的,可以去了解下,移步官方中文文档: R ...

  4. VS开发C语言系列(零)-VS2013写C语言错误汇总

    错误代码 error C3861:调用函数前未引用 error C4996:调用不安全的函数 error C2668:重载函数不明确 error C3861:"文件名" 找不到标识 ...

  5. bootstrap 字体颜色 对齐方式

    一:字体代码:作用--颜色 1..text-muted:提示--浅灰色 2..text-primary:主要--蓝色 3..text-success:成功--浅绿色 4..text-info:     ...

  6. WinServer2008配置任务计划

    window server 2008下配置任务计划 打开window servers 2008下任务计划配置工具 点击“开始”-->“管理工具”-->“任务计划程序”,打开任务计划配置工具 ...

  7. java中 数组 list map之间的互转

    三者之间转换关系,一张图清晰呈现. 上代码: 其中的maputils是apache的collection包. package util; import java.util.ArrayList; imp ...

  8. TCP:三次握手,URG、ACK、PSH、RST、SYN、FIN 含义

    http://blog.csdn.net/wudiyi815/article/details/8505726 TCP:SYN ACK FIN RST PSH URG简析   三次握手Three-way ...

  9. Discuz! X3.1云平台QQ互联的Unknown column 'conuintoken' in 'field list' 解决办法

    http://www.discuz.net/thread-3482497-1-1.html 由于程序安装默认数据表的结构和QQ互联数据表结构不同,安装Discuz! X3.1和升级Discuz! X3 ...

  10. CAD在网页中打印的图纸里面添加页眉及页脚

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...