【一天一道LeetCode】#242. Valid Anagram
一天一道LeetCode
本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github
欢迎大家关注我的新浪微博,我的新浪微博
欢迎转载,转载请注明出处
(一)题目
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的有效字谜
解题思路:有效字谜是指t是由s中的字符改变相对位置后组成的字符串。
84ms解题版本:
class Solution {
public:
bool isAnagram(string s, string t) {
if(s.length()!=t.length()) return false;//长度不等,直接返回false
sort(s.begin(),s.end());//排序
sort(t.begin(),t.end());
return s==t?true:false;//判断是否相等
}
};
12ms的版本:
用哈希表,首先遍历s,记录每个字符出现的次数,然后遍历t,出现某个字符就次数就减1,判断最后的次数是否都为0
class Solution {
public:
bool isAnagram(string s, string t) {
int count[26] = {0};
for(int i = 0 ; i < s.length();i++) count[s[i]-'a']++;
for(int i = 0 ; i < t.length();i++) count[t[i]-'a']--;
for(int i = 0 ; i < 26 ; i++) if(count[i]!=0) return false;
return true;
}
};
【一天一道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: ...
- 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(有效的变位词)
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 ...
随机推荐
- 《Java技术》第三次作业--面向对象——继承、抽象类、接口
1.阅读下面程序,分析是否能编译通过?如果不能,说明原因.应该如何修改?程序的运行结果是什么?为什么子类的构造方法在运行之前,必须调用父 类的构造方法?能不能反过来? class Grandparen ...
- C语言程序设计第二次作业—————顺序结构改
1.输出带框文字:在屏幕上输出以下3行信息. ************* Welcome ************* 源程序 #include <stido.h> int mian() { ...
- Selenium之unittest测试框架详谈及实操
申明:本文是基于python3.x及selenium3.x. unittest,也可以称为PyUnit,可以用来创建全面的测试套件,可以用于单元自动化测试(模块).功能自动化测试(UI)等等. 官方文 ...
- 【docker简易笔记】docker基础信息的分享
docker 使用的频率越来越高,所以在后续的一些博客中会分享一些docker的安装和使用. 一.docker介绍 "Docker 最初是 dotCloud 公司创始人 Solomon ...
- 前端开发利器VSCode
最近找到一款非常好用的开发利器,VSCode.一直认为微软做的东西都很一般,这个软件让我刮目相看了. 之前使用webstorm卡的不行,换了这个非常好用. 用着还不错,这里记录下一些使用的心得. VS ...
- C# 异步转同步
当我们的程序运行时,调用了一段异步的逻辑A,这段异步的逻辑无法转化为同步(如动画.下载进度等) 而,我们又需要等待异步逻辑A处理完成,然后再执行其它逻辑B. 那就迫切需要将异步转同步了! //参数bo ...
- 分享一个二维码图片识别控制台程序Demo
怎么用NuGet和配置log4net就不介绍了,直接上代码(QRDecodeDemo.zip). (Visual Studio 2015 下的项目,用的.NET Framework 4.5.2) 吐槽 ...
- Cookie&Session(会话技术)
一.Cookie技术 从打开一个游览器访问某个站点,到关闭这个游览器的整个过程成为一次会话 会话技术分为Cookie和Session Cookie:数据存储在客服端本地,减少对服务端的存储的压力,安全 ...
- PHP XML SimpleXML
PHP 可以基于 SimpleXML 生成和解析 xml 的方法,通过本节的实例,你将了解 PHP 是如何使用 SimpleXML 生成及解析 xml 格式数据的. PHP SimpleXML 处理最 ...
- 安卓7.1 新特性Shortcut
介绍 Shortcut 是谷歌在API25提出来的 类似苹果3D touch 但是没有压力感应.在安卓中完全就是长按. 来看下效果吧: 是不是很赞? 那么请随本文一起学习吧 更新 新建项目 在你项目下 ...