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. c#winform选择文件,文件夹,打开指定目录方法

    private void btnFile_Click(object sender, EventArgs e) { OpenFileDialog fileDialog = new OpenFileDia ...

  2. iOS 滑动性能优化

    iOS 滑动性能优化 目录 一. 减少图层的Blend操作 1. UIView的背景色避免使用clearColor 2. 控件贴图避免使用带alpha的图片 3. UIImageView 使用时避免半 ...

  3. js改变HTML元素的值

    js改变HTML元素的值(常用,备忘) <!DOCTYPE html> <html> <body> <h1>我的第一段 JavaScript</h ...

  4. src 小心得

    关于src的引用,不要用相对路径,  ../   虽然省事,但是跳转页面时容易出错. 举个例子: 在web页面引用  D:\phpStudy\WWW\ueditor\utf8-php 这个文件夹下面  ...

  5. Ubuntu 开机进入命令行模式

    1.修改配置 sudo vim /etc/default/grub 把 GRUB_CMDLINE_LINUX_DEFAULT="quiet splash" 改为 GRUB_CMDL ...

  6. ajax异步提交数据动态更改select选项

    <!DOCTYPE html> <html> <head> <title></title> <script src="../ ...

  7. C#GDI+编程基础(一:Graphics画布类)

    GDI+存在的意义:将变成与具体硬件实现细节分开. GDI+步骤:获取画布,绘制图像.处理图像 命名空间: using System.Drawing;//提供对GDI+基本图形功能的访问 using ...

  8. PHPCMS系统常量

    以下系统常量全局可用 APP_PATH  动态程序路径 WEB_PATH  网站根路径 JS_PATH  JS路径 CSS_PATH CSS路径 IMG_PATH  图片路径 CACHE_PATH 缓 ...

  9. 9 patch png 的上下左右

    9 patch png 的上下左右   前言: 9 patch png 图片,扩展名为.9.png,是一个标准的PNG图像,它包括额外的1个像素的边界,通过对这个边界的描述来达到我们预期的拉伸效果.a ...

  10. HDU 5023 A Corrupt Mayor's Performance Art(线段树区间更新)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5023 解题报告:一面墙长度为n,有N个单元,每个单元编号从1到n,墙的初始的颜色是2,一共有30种颜色 ...