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 ...
随机推荐
- 2014年国人开发的最热门的.NET开源项目 TOP 25
原文地址:http://www.cnphp6.com/archives/72213 1 奎宇工作室 / DotNetCodes C# 一些常用的功能性代码,可以减少许多开发时间,而且类与类之间没有什么 ...
- Linux 下文件名乱码(无效的编码)的解决办法
文件是在WIndows 下创建的,Windows 的文件名中文编码默认为GBK,而Linux中默认文件名编码为UTF8,由于编码 不一致所以导致了文件名乱码的问题,解决这个问题需要对文件名进行转码. ...
- win10+vs2013+Qt5.4 安装方法
环境:win10 cn_visual_studio_ultimate_2013_x86_dvd_3009109 qt-opensource-windows-x86-msvc2013_64_opengl ...
- MVC Razor 语法(转)
http://blog.csdn.net/pasic/article/details/7072340 原文地址:MVC Razor 语法(转)作者:panzhaowen_jacki 语法名称 Razo ...
- 新手学习 Vim 的五个技巧
多年来,我一直想学 Vim.如今 Vim 是我最喜欢的 Linux 文本编辑器,也是开发者和系统管理者最喜爱的开源工具.我说的学习,指的是真正意义上的学习.想要精通确实很难,所以我只想要达到熟练的水平 ...
- EasyBCD安装CentOS双系统
之前使用CentOS安装过ubuntu,今天安装一次CentOS6.6,要复杂一些,列文备忘. 1.安装EasyBCD,下载CentOS-6.6-x86_64-bin-DVD1.iso.CentOS- ...
- js难点之闭包理解
如何从外部读取局部变量? 闭包就是能够读取其他函数内部变量的函数. 由于在Javascript语言中,只有函数内部的子函数才能读取局部变量,因此可以把闭包简单理解成“定义在一个函数内部的函数”. 所以 ...
- 编写一个Java应用程序,该程序包括3个类:Monkey类、People类和主类 E。要求: (1) Monkey类中有个构造方法:Monkey (String s),并且有个public void speak() 方法,在speak方法中输出“咿咿呀呀......”的信息。 (2)People类是Monkey类的子类,在People类中重写方法speak(),在speak方法 中输出“小样的,不
package homework1; public class Monkey { //构造方法 Monkey(String s) { } //成员方法 public void speak() { Sy ...
- java 实现视频转换通用工具类:视频相互转换-Ffmpeg(三)
java 实现视频转换通用工具类:获取视频元数据信息(一) java 实现视频转换通用工具类:视频相互转换-总方法及Mencoder(二) 这节主要是ffmpeg的相关方法封装,在实际调用中主要使用f ...
- 利用UIBezierPath实现一个带圆角的视图
- (void)drawRect:(CGRect)rect { // draw a box with rounded corners to fill the view - UIBezierPath * ...