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 ...
随机推荐
- CentOS安装nvidia显卡驱动
1.下载 nvidia 相应的驱动: 2.修改/etc/modprobe.d/blacklist.conf文件,在里面加入blacklist nouveau. 3.重建image $ mv /boot ...
- acm-字符串整理
一.后缀数组 #define maxn 200015 int wa[maxn],wb[maxn],wv[maxn],WS[maxn]; int len, sa[maxn] ; inline void ...
- Windows性能监视器之CPU、硬盘、IO等监控方法详解-摘自网络
一般操作系统性能主要涉及到的问题主要有:处理器使用情况.内存占有量.磁盘I/0操作以及网络流量等. 查看Windows性能情况,大部分情况下是通过 “Windows任务管理器”,可以通过在 ”命令行” ...
- adb pull命令复制android数据库文件.db到电脑
1.win+r cmd进入命令行 2.cd 进入[sdk]/platform-tools目录下 3.执行下面命令行,复制xxx.db到F:/dest adb pull /data/data/[pack ...
- Objective-C Runtime 运行时之二:成员变量与属性
类型编码(Type Encoding) 作为对Runtime的补充,编译器将每个方法的返回值和参数类型编码为一个字符串,并将其与方法的selector关联在一起.这种编码方案在其它情况下也是非常有用的 ...
- 一种从JSON数据创建Java类的高效办法
<一种从JSON数据创建Java类的高效办法> 作者:chszs,转载需注明.博客主页:http://blog.csdn.net/chszs JSON格式的数据经常会遇到,比如调用Web服 ...
- 知方可补不足~SQL数据库用户的克隆,SQL集群的用户同步问题
我们知道在为sqlserver建立功能数据库时,通过会为库再建立一个登陆名,而这个登陆名时,只用来管理这个数据库,这是安全的,正确的.
- JavaScript 要点(十六)RegExp 对象
RegExp:是正则表达式(regular expression)的简写. RegExp 对象 正则表达式是描述字符模式的对象. 正则表达式用于对字符串模式匹配及检索替换,是对字符串执行模式匹配的强大 ...
- IOS开发之 ---- 苹果系统代码汉字转拼音
NSString *hanziText = @"我是中国人"; if ([hanziText length]) { NSMutableString *ms = [[ ...
- js与java通信
js 调用java中的接口并传递参数给客户端处理方式: webView.addJavascriptInterface(new NewsDetail() , "newsDetail" ...