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 = "anagram", t = "nagaram", return true.
s = "rat", t = "car", return false.
Note: You may assume the string contains only lowercase alphabets.
Follow up: What if the inputs contain unicode characters? How would you adapt your solution to such case?
题目大意:
给定两个字符串s和t,写一个函数来确定t是不是s的一个变位词。
比如说:s="anagram", t="nagaram", 是
s="rat", t="car", 不是
注意:你可以假定字符串只含有小写字母。
进一步的:如果输入含有unicode字符呢?这样的情况怎么去适用你的解决方案呢?
解法一:排序后判相等
class Solution {
public:
bool isAnagram(string s, string t){
sort(s.begin(), s.end());
sort(t.begin(), t.end());
return s==t;
}
};
runtimes:80ms
复杂度为O(nlogn);
解法二:哈希表,判断两个字符串相同字母的个数是否相等
class Solution {
public:
bool isAnagram(string s, string t) {
int len_s = s.length(), len_t = t.length(), i, ns[] = {};
//vector<int> ns(26, 0);
for(i = ; i < len_s; i++)
ns[s[i] - 'a']++;
for(i = ; i < len_t; i++)
ns[t[i] - 'a']--;
for(i = ; i < ; i++)
if(ns[i] != )
return false;
return true;
}
};
runtimes:12ms
复杂度:O(n)
Leetcode 242. Valid Anagram(有效的变位词)的更多相关文章
- 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 ...
- 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. Example 1: Input: ...
- LeetCode 242 Valid Anagram
Problem: Given two strings s and t, write a function to determine if t is an anagram of s. For examp ...
- (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 pytyhon
题目: Given two strings s and t, write a function to determine if t is an anagram of s. For example,s ...
随机推荐
- Javascript时间操作小结
来源:http://www.ido321.com/847.html 在项目需要一个计时器,效果如下: js代码 1: /*获取当前时间*/ 2: function getCurrentDate() 3 ...
- MapReduce 支持的部分数据挖掘算法
MapReduce 支持的部分数据挖掘算法 MapReduce 能够解决的问题有一个共同特点:任务可以被分解为多个子问题,且这些子问题相对独立,彼此之间不会有牵制,待并行处理完这些子问题后,任务便被解 ...
- powerdesign的license key到期,解决办法
到2013年9月24日为止我把这文件覆盖了都是行的!不行的请留言说明下! 下载地址:powerdesigner license key 15.1 找到安装目录,直接覆盖就行了!
- textarea使用注意事项
问题现象: 意外的发现页面中 textarea 标签中的内容缩进了 猜测: CSS影响了? 过程:(辛酸得说说) 查了CSS,并没有发现,CSS是正常的 然后找了一个正常的,跟这个异常的进行了对比,代 ...
- HDU-2262 Where is the canteen 概率DP,高斯消元
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2262 题意:LL在一个迷宫里面转,每次走向周围能走的点的概率都是一样的,现在LL要随机的走到cante ...
- hdu 3617 Happy 2009
Happy 2009 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- 多个分布式系统如何共享使用一个固定公网IP
传统的做法,一个分布式业务系统就有一个中间件,一个中间件需要使用至少一个固定公网IP,这样的话,多个业务系统就需要使用多个固定公网IP. 大家知道,固定公网IP价格可是不菲的.能不能让多个分布式业务系 ...
- [iOS基础控件 - 5.4] 广告分页代码(UIScrollView制作)
A.概念 例子就是桌面的APP列表,当APP数量超过一个屏幕,自动进行分页 B.实现思路 1.创建一个UIScrollView,这里设置为宽度跟屏幕相同,高度1/4屏幕高度左右 2.使用代码在UI ...
- Struts2中DMI(动态方法调用)的错误问题(There is no Action mapped for namespace [/xxx] and action name [xxx!yyy] a)
默认的Struts.xml中是这样的 <constant name="struts.enable.DynamicMethodInvocation" value="f ...
- 剑指OFFER之栈的压入、弹出序列(九度OJ1366)
题目描述: 输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否为该栈的弹出顺序.假设压入栈的所有数字均不相等.例如序列1,2,3,4,5是某栈的压入顺序,序列4,5,3,2,1是该压栈 ...