leetcode面试准备:Valid Anagram
leetcode面试准备:Valid Anagram
1 题目
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.
接口: public boolean isAnagram(String s, String t)
2 思路
题意
对比两个字符串是否是一样的。
解
HashMap思想存储字符,字符串s
往map里面存储,字符串t
往map里面取。最后看map
是否为空:if 空,true
.
复杂度: Time: O(n) , Space:O(26)
3 代码
public boolean isAnagram(String s, String t) {
Map<Character, Integer> smap = new HashMap<Character, Integer>();
int slen = s.length();
int tlen = t.length();
if (slen != tlen)
return false;
for (int i = 0; i < slen; i++) {
Character c = s.charAt(i);
if (smap.containsKey(c)) {
int count = smap.get(c);
count++;
smap.put(c, count);
} else {
smap.put(c,1);
}
}
for (int i = 0; i < tlen; i++) {
Character c = t.charAt(i);
if (smap.containsKey(c)) {
int count = smap.get(c);
count--;
if(count == 0) {
smap.remove(c);
} else {
smap.put(c, count);
}
} else {
return false;
}
}
return smap.isEmpty();
}
4 总结
HashMap思想。
leetcode面试准备:Valid Anagram的更多相关文章
- 【LeetCode】242. Valid Anagram (2 solutions)
Valid Anagram Given two strings s and t, write a function to determine if t is an anagram of s. For ...
- 【一天一道LeetCode】#242. Valid Anagram
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given t ...
- LeetCode算法题-Valid Anagram(Java实现)
这是悦乐书的第198次更新,第205篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第61题(顺位题号是242).给定两个字符串s和t,写一个函数来确定t是否是s的anag ...
- LeetCode OJ: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 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 字典统计词频 排序 日期 [LeetCode] 题目地址:ht ...
- 【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 OJ 之 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)
242. 有效的字母异位词 LeetCode242. Valid Anagram 题目描述 给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的一个字母异位词. 示例 1: 输入: s ...
- 22. leetcode 242. Valid Anagram(由颠倒字母顺序而构成的字)
22. 242. Valid Anagram(由颠倒字母顺序而构成的字) Given two strings s and t, write a function to determine if t i ...
随机推荐
- Atom 插件安装
“webstom” 目前还没免费版的 不过类似的倒是有个! 首先想到的一句话是:还在为webstom不是正版而发愁吗? 其实很小的时候我们的世界是非黑即白的,但慢慢长大后,发现其实还有灰色的存在! 工 ...
- android线程池ThreadPoolExecutor的理解
android线程池ThreadPoolExecutor的理解 线程池 我自己理解看来.线程池顾名思义就是一个容器的意思,容纳的就是ThreadorRunable, 注意:每一个线程都是需要CPU分配 ...
- Windows Server 2008安装Memcached笔记
分布式缓存系统Memcached简介与实践 缘起: 在数据驱动的web开发中,经常要重复从数据库中取出相同的数据,这种重复极大的增加了数据库负载.缓存是解决这个问题的好办法.但是ASP.NET中的虽然 ...
- java web -部署在linux
概述: 初次将java web项目部署到linux上, 还是很顺利的, 基本上没有什么错误. 步骤: 1, 安装jdk(官网中说了很清晰了),在linux上安装安装jdk, 不想windows那样, ...
- 轻松应对C10k问题
http://blog.csdn.net/u011011917/article/details/17203539 传统的.教科书里的I/O复用等待函数select/poll在处理数以万计的客户端连接时 ...
- 关于bootstrap的datepicker在meteor应用中的使用(不包含bootstrap框架)
1.安装bootstrap3-datepicker包 meteor add rajit:bootstrap3-datepicker 2.使用方法 Example In your handlebars ...
- STL库之单链表:forward_list
class template forward_list <forward_list> template < class T, class Alloc = allocator<T ...
- Linux crontab定时执行任务 命令格式与详细例子
基本格式 : * * * * * command 分 时 日 月 周 命令 第1列表示分钟1-59 每分钟用*或者 */1表示 第2列表示小时1-23(0表示0点) 第3列表示日期1-31 第4列表示 ...
- 修改win8系统中启动管理器的系统引导信息
最近用某软件做了个启动U盘,软件安装在电脑上,启动盘很快做完了,结果重启电脑的时候发现悲剧,windows启动后会显示出一个系统引导菜单,显示有3秒倒计时但是倒计时结束依然不能自动进入系统.. 然后. ...
- Omnithreadlibary学习(3)-IOmniTask异步执行SendMessage
在任务中发送消息, 可以是函数或者对象方法 TOmniTaskMessageEvent = procedure(const task: IOmniTaskControl; const msg: TOm ...