LeetCode 242 Valid Anagram
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的更多相关文章
- 22. leetcode 242. Valid Anagram(由颠倒字母顺序而构成的字)
		22. 242. Valid Anagram(由颠倒字母顺序而构成的字) Given two strings s and t, write a function to determine if t i ... 
- 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 ... 
- [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: ... 
- 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 ... 
- [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: ... 
- (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 ... 
- 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, ... 
- 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 = & ... 
- 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 ... 
随机推荐
- Jquery 操作IFrame
			使用jquery操作iframe 1. 内容里有两个ifame <iframe id="leftiframe"...</iframe> <iframe id ... 
- 笔记之Python网络数据采集
			笔记之Python网络数据采集 非原创即采集 一念清净, 烈焰成池, 一念觉醒, 方登彼岸 网络数据采集, 无非就是写一个自动化程序向网络服务器请求数据, 再对数据进行解析, 提取需要的信息 通常, ... 
- 关于Html编码问题,例如字符:·
			我写的WCF服务突然报错了... 然后我发现传过来的字符不完整 {"完整":"尼古拉·奥斯特洛夫斯基的信息"} 然后传过来的是:{"完整": ... 
- Problem B    Boxes in a Line
			省赛B题....手写链表..其实很简单的.... 比赛时太急了,各种手残....没搞出来....要不然就有金了...注:对相邻的元素需要特判..... Problem B Boxes in a Li ... 
- 64.SHELL
			SHELL 1. crontab定时器 编辑使用crontab -e 一共6列,分别是:分 时 日 月 周 命令 查看使用crontab -l 删除任务crontab -r 查看crontab执行日志 ... 
- 类调用类的protected或private的成员函数或成员变量
			1.在其中一个类定义友元函数,则可以实现该类直接使用另外类的里所有内容. 一般实例化两个类,友元类以及自身类,实现友元类传递指针到自身类 2.如果两个类是可以继承的关系,则在子类里继承该类,实现在子类 ... 
- Java网络编程学习
			服务器是指提供信息的计算机或程序,客户机是指请求信息的计算机或程序,而网络用于连接服务器与客户机,实现两者相互通信.但有时在某个网络中很难将服务器与客户机区分开.我们通常所说的“局域网”(Local ... 
- ubutu之jdk安装
			1.jdk下载 http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html 2.解压jdk- ... 
- mysql常用语句总结
			1.创建语句 CREATE DATABASE database_name //创建数据库 //删表 DROP TABLE IF EXISTS `t_social_user_extend`; //建表C ... 
- H5图像遮罩-遁地龙卷风
			(-1)写在前面 这个idea不是我的,向这位前辈致敬.我用的是chrome49.用到的图片资源在我的百度云盘里http://yun.baidu.com/share/link?shareid=1970 ... 
