Problem:

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.

Summary:

给出字符串s和t,判断t是不是s的相同字母异序词。

Analysis:

1. Hash表,记录s中字母出现的次数,再判断t中的字母是否有相同出现频率。此方法也可以由map实现。

 class Solution {
public:
bool isAnagram(string s, string t) {
int len1 = s.size(), len2 = t.size(), ch[] = {}; for (int i = ; i < len1; i++) {
int tmp = s[i] - 'a';
ch[tmp]++;
} for (int i = ; i < len2; i++) {
int tmp = t[i] - 'a';
ch[tmp]--;
} for (int i = ; i < ; i++) {
if (ch[i]) {
return false;
}
} return true;
}
};

2. 分别给两字符串中字符由小到大排序,判断排序后两字符串是否相等即可。但这个方法效率较低。

 class Solution {
public:
bool isAnagram(string s, string t) {
sort(s.begin(), s.end());
sort(t.begin(), t.end()); return s == t ? true : false;
}
};

LeetCode 242 Valid Anagram的更多相关文章

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

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

  2. LN : leetcode 242 Valid Anagram

    lc 242 Valid Anagram 242 Valid Anagram Given two strings s and t, write a function to determine if t ...

  3. [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: ...

  4. 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 ...

  5. [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: ...

  6. (easy)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 ...

  7. Java [Leetcode 242]Valid Anagram

    题目描述: Given two strings s and t, write a function to determine if t is an anagram of s. For example, ...

  8. 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 = & ...

  9. Leetcode 242 Valid Anagram pytyhon

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

随机推荐

  1. [译]git init

    git init git init命令用来创建一个新的Git仓储.可以用在一个已经存在的但是没有受Git版本控制的项目,或者用来初始化一个全新的没有任何文件的空仓储.git init通常是你开始一个新 ...

  2. 湖南附中模拟day1 瞭望塔

    /* 这个题要用到树的性质,一般比较难的图论题会往这方面靠拢,这样用很容易出错,应该先写暴力,然后再去一点点想正解 */ //暴力70分 #include<iostream> #inclu ...

  3. Javascript软键盘设计

    国内大多数网站的密码在网络传输过程中都是明文的,我们目前正在做的产品也是这样的情形,这正常吗? 大家都偷懒?不重视安全?各人持有观点,有人认为明文传输并不是想象中的那么可怕,事实上正常情况下这些报文你 ...

  4. Ubuntu10.04下安装Ns2的一系列错误及解决方案

    安装之前改一下nam1.11下的agent.h文件73行 Null改为0 第一个错误: xxx configuration: Syntax error: Unterminated quoted str ...

  5. JS补充

    JavaScript JavaScript 使用那些老旧的实例可能会在 <script> 标签中使用 type="text/javascript".现在已经不必这样做了 ...

  6. html5开发制作,漂亮html5模板欣赏,H5网站建设

    html5是什么? HTML5 是下一代的 HTML(超文本标记语言,网页的组成部分),HTML5是web开发世界的一次重大的改变,能适配pc.手机等各终端,跨平台性能极强,移动互联网是未来的趋势,h ...

  7. Octal Fractions

    Octal Fractions Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 149  Solved: 98 Description Fractions ...

  8. Spring 事务知识

    1.1  Spring注解的各种行为 事物传播注解: @Transactional(propagation=Propagation.REQUIRED) (常用) 如果有事务, 那么加入事务, 没有的话 ...

  9. Leonbao:MapKit学习笔记

    以下仅作了解, 实际使用以百度地图居多, 因为百度地图有动态路径规划等接口 MapKit学习笔记    原帖: http://www.cocoachina.com/bbs/read.php?tid-6 ...

  10. Longest Common Subsequence

    Given two strings, find the longest common subsequence (LCS). Your code should return the length of  ...