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 ...
随机推荐
- BNUOJ-26475 Cookie Selection 堆,线段树等
题目链接:http://www.bnuoj.com/bnuoj/problem_show.php?pid=26475 题意:每次输入一个操作,如果是数字,那么放入一个容器中,如果是#号,取出当前容器中 ...
- SGU131 - Hardwood floor(状态压缩DP)
题目大意 给定一个N*M大小的矩形,要求你用1*2和2*2(缺个角)的砖块把矩形铺满(不能重叠),问总共有多少种铺法? 题解 受POJ2411的影响,怎么都没想到3,4,5,6这几种情况该怎么放置,看 ...
- python basic programs
- 【组队训练】2016 ACM/ICPC Asia Regional Dalian Online
因为不是一队……毫无晋级的压力……反正有压力也进不去呵呵呵…… 开场zr看1006我看1010.. 1010我一直在wa... zr的1006倒是比较轻松的过了...然后我让他帮我看10.... 跟他 ...
- _int、NSInteger、NSUInteger、NSNumber的区别和联系
1.首先先了解下NSNumber类型: 苹果官方文档地址:https://developer.apple.com/library/ios/documentation/Cocoa/Reference/F ...
- Javascript注意事项二【避免误用parseInt】
parseInt("123abc"); //123parseInt("1.73"); //1parseInt(".123"); //NaN ...
- The Sorrows of Young Werther
The Sorrows of Young Werther J.W. von Goethe Thomas Carlyle and R.D. Boylan Edited by Nathen Haskell ...
- 实例:ABAP权限对象设计与权限检查的实现(详细)
学习总结,分享给大家,,,(有图有真像) 我在ECC里创建了一张表,随意插入了5条数据 创建权限对象,使分配这个权限的用户只能操作部门编号(edept)为 ‘10’ 的数据. 1. SU20,创建权限 ...
- XMPP——Smack[6]离线消息和离线文件的实现
终篇,三天所学所用,也就这些,如果需要大家要自己去查资料研究研究,功能其实可以很强大的 可惜界面做得不好,一大短处,从大一迄今没整好,主要是个人审美不行,哎 毕业季呀毕业季,明天摆摊卖书,再半月就可能 ...
- 作为平台的Windows PowerShell(二)
在此系列文章的前一篇,我们看到了怎样使用System.Management.Automation.PowerShell 类来在c#应用程序中运行PowerShell 命令.在那些例子中,我们创建的都是 ...