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 ...
随机推荐
- log4net的基本配置及用法
[1].[代码] [C#]代码 跳至 [1] [2] ? 1 2 using System.Reflection; //使用反射 static private ILog log = log4net. ...
- POJ 2155 Matrix
二维树状数组.... Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissio ...
- HDOJ 3593 The most powerful force
树形DP / 泛化物品的背包...可以去看09年徐持衡论文<浅谈几类背包问题> The most powerful force Time Limit: 16000/8000 MS (Jav ...
- 微信公众平台中添加qq在线聊天代码
微信公众平台是个不错的媒体,可以和你的小伙伴们即时交流,但你的小伙伴们是用手机上的微信,打字自然就慢了:有人说用微信网页版,那个也不习惯,再说也不一定所有人都知道网页版微信.(2014.01.22更新 ...
- 怎样用Lodrunner测试WAP站点的性能(两种解决方案)
其实用IE就可以的!!!! 1.借助opera实现对WAP站点的录制 第一:安装opera软件 第二:Lodrunner选择Web(HTTP/HTML)协议 第三:Lodrunner的Applicat ...
- bootstrap-tab
功能:点击时切换相应的内容或图片 插件:tab.js 要点:tab标签用在导航条上,以data-toggle作被点击者, 以tab-content作内容显示 <!DOCTYPE html> ...
- 一个C#的与web服务器交互的HttpClient类
using System; using System.Collections.Generic; using System.IO; using System.Text; using System.Net ...
- 纯CSS多级菜单
主要代码部分: /*新增的二级菜单部分*/ .menu ul ul { visibility:hidden;/*开始时是隐藏的*/ position:absolute; left:0px; top:3 ...
- Java设计模式 之 命令模式
1 从属模式分类 行为性模式 2 命令模式意图 命令模式可将动作的请求者和动作的执行者对象中解耦. 该模式将一个行为操作发起者的请求封装到对象中,该请求由另外一个对象执行. 将动作 ...
- HNU 12886 Cracking the Safe(暴力枚举)
题目链接:http://acm.hnu.cn/online/?action=problem&type=show&id=12886&courseid=274 解题报告:输入4个数 ...