题目:

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. easyui form.rest和clear 重置表单和清除表单数据区别

    easyui中的一般我们在新增和编辑的时候 都是用一个form表单 那新增的时候 需要重置下表单内容,一般用 $('#EditForm').form('reset'); 大部分时候没问题,但是如果表单 ...

  2. 如何快速获取yun2win app key?

    注册yun2win开发者账号 1.在注册页面输入您的邮箱,点击下方发送,yun2win将会发送一封验证邮件到您的邮箱: 2.如果没有收到邮件请查看垃圾箱或者点击重新发送: 3.打开邮箱查看验证邮件,点 ...

  3. spring 将配置文件中的值注入 属性

    1.编写配置文件 #债权转让 #默认周期 必须大于0 credit.defaultDuration=1 #最小转让金额(元) credit.minBidAmount=1.00 #最小转让时间 到期时间 ...

  4. Oracle基础理论笔记(一):模式概念

    ---oracle 10g 1.在oracle数据库中,数据对象是以模式为单位进行组织和管理的.模式是指一系列的逻辑数据结构或对象的集合. 2.模式与用户名相对应,一个模式只能对应一个用户,并且该模式 ...

  5. 3.用Redis Desktop Manager连接Redis(CentOS)

    Redis Desktop Manager是Redis图形化管理工具,方便管理人员更方便直观地管理Redis数据. 然而在使用Redis Desktop Manager之前,有几个要素需要注意: 一. ...

  6. CSS之float浮动

    CSS理解之float浮动 首先我们看看W3C给出的关于 float 的说明: 参考资料   MDN   W3C

  7. HDU_5734_数学推公式

    题意:给一个向量W={w1,w2……,wn},和一个向量B,B的分量只能为1和-1.求||W-αB||²的最小值. 思路:一来一直在想距离的问题,想怎么改变每一维的值才能使这个向量的长度最小,最后无果 ...

  8. SQL几种常用的函数

    函数的种类: 算数函数(数值计算的函数) 字符串函数(字符串操作的函数) 日期函数(用来进行日期操作的函数) 转换函数(用来转换数据类型和值的函数) 聚合函数(用来进行数据聚合的函数) 算数函数(+- ...

  9. ubuntu14.0开机guest账号禁用方法

    在终端里进入/usr/share/lightdm/lightdm.conf.d/目录 sudo vim 50-unity-greeter.conf 然后在文件里输入: [SeatDefaults] a ...

  10. MySQL操作数据库和表的基本语句(DDL

    1.创建数据库: CREATE DATABASE 数据库名; eg.CREATE DATABASE test_ddl;122.创建表 CREATE TABLE 表名(列名 数据类型 约束,...); ...