【一天一道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 ...
随机推荐
- mysql的连接处理过程
在mysqld_main函数中经过一系列的初始化后,mysql开始监听客户端的连接 mysqld_socket_acceptor->connection_event_loop(); 查看my ...
- net use命令详解
net use命令详解 1)建立空连接: net use \\IP\ipc$ "" /user:"" (一定要注意:这一行命令中包含了3个空格) 2)建立非空连 ...
- hibernate4整合spring3出现java.lang.NoClassDefFoundError: [Lorg/hibernate/engine/FilterDefinition;
解决办法 原先:<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annota ...
- eclipse的maven操作无反应
第一 查eclipse能不能正常用 hi world.java 第二 查maven能不能正常用 cmd: mvn -v 第三 看看maven和eclipse是不是64位之类的 第四 maven和ecl ...
- 关于Intellij Idea导出可执行打jar
今天被人问到如何导jar包出来,mark一下Intellij Idea的简单方式. 1.菜单:File->project stucture 2.在弹窗最左侧选中Artifacts->&qu ...
- 服务器&阵列卡&组raid 5
清除raid信息后,机器将会读不到系统, 后面若进一步操作处理, raid信息有可能会被初始化掉,那么硬盘数据就有可能会被清空, 导致数据丢失, 否则如果只是清除raid信息,重做raid是可以还原系 ...
- Oracle中case用法总结
--case语句的种类: .简单case语句 语法: case exp when comexp then returnvalue ... when comexp then returnvalue el ...
- day08 JSP
day08 JSP 1. jsp 入门和 jsp 运行原理 2. jsp 语法 2.1 jsp 模板元素:jsp 页面中的 html 内容.它定义了网络基本骨架,即定义了页面结构和外观. 2.2 js ...
- hiredis的各种windows版本
hiredis的各种windows版本(金庆的专栏 2016.12)hiredis 是内存数据库 redis 的客户端C库, 不支持Windows.hiredis的Windows移植版本有许多:des ...
- Zookeeper核心工作机制(zookeeper特性、zookeeper数据结构、节点类型)
10.1 zookeeper特性 1.Zookeeper:一个leader,多个follower组成的集群. 2.全局数据一致:每个server保存一份相同的数据副本,client无论连接到哪个ser ...