题目:

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. Hadoop Hive概念学习系列之hive里的优化和高级功能(十四)

    在一些特定的业务场景下,使用hive默认的配置对数据进行分析,虽然默认的配置能够实现业务需求,但是分析效率可能会很低. Hive有针对性地对不同的查询进行了优化.在Hive里可以通过修改配置的方式进行 ...

  2. java DDD 基于maven开发的探讨

    对于DDD我目前的理解是 1.除了数据的基本操作,也可以把一些公用的方法或者类迁移到Infrastructrue 2.对于domain层可以声明各个聚合根的操作接口:例:IXXXRepository ...

  3. Struts2框架实现简单的用户登入

    Struts框架汲取了Struts的优点,以WebWork为核心,拦截器,可变和可重用的标签. 第一步:加载Struts2 类库: 第二步:配置web.xml <?xml version=&qu ...

  4. JavaScript面试题链接汇总

    最新JavaScript笔试题(含答案) - 爱思资源网 前端工程师面试问题列表 - 爱思资源网 腾讯最新前端面试题记录分享 - 爱思资源网 优酷前端JS部分面试题 - 爱思资源网 百度校园招聘web ...

  5. nagios 插件ndoutils 安装配置

    nagios 插件ndoutils 安装配置 原文地址:http://www.cnblogs.com/caoguo/p/5022645.html # Nagios install ndoutils # ...

  6. GridView中字符串太长处理方式

    <asp:TemplateField HeaderText="子机构编号"> <ItemTemplate> <asp:Label ID="L ...

  7. Split()函数

    最近遇到一个有趣的问题关于使用Split函数 ,该函数能够根据传递的参数拆分,并返回一个string的数组. 贴出一个奇怪的例子 using System; using System.Collecti ...

  8. .htaccess使用

    RewriteEngine on #请求替换 #test-zhangsan-20 替换为 test.php?name=zhangsan&age=20 RewriteRule test-([a- ...

  9. adjtimex修改tick值用法举例

    测试 用户态下内核时钟计数间隔,默认都是100HZ.因此单个tick代表了10^4 us. 可以设置每个tick代表的时钟长度,因此把tick增加1(即增加为10001us)的影响是每天时间快8.64 ...

  10. enote笔记语言(2)(ver0.2)

    why not(whyn't)                     为什么不(与“why”相对应,是它的反面)   how对策 how设计   key-memo                   ...