Valid Anagram 解答
Question
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.
(An anagram is a type of word play, the result of rearranging the letters of a word or phrase to produce a new word or phrase, using all the original letters exactly once; for example Torchwood can be rearranged into Doctor Who.)
Solution
Use hashmap to count each character's appearance time. Time complexity O(n), space cost O(n).
Note here, counts.put(tmp, count.get(tmp)--); is wrong!
public class Solution {
public boolean isAnagram(String s, String t) {
if (s == null || t == null)
return false;
if (s.length() != t.length())
return false;
Map<Character, Integer> counts = new HashMap<Character, Integer>();
int length = s.length();
for (int i = 0; i < length; i++) {
char tmp = s.charAt(i);
if (counts.containsKey(tmp)) {
int count = (int)counts.get(tmp);
counts.put(tmp, count + 1);
} else {
counts.put(tmp, 1);
}
}
for (int i = 0; i < length; i++) {
char tmp = t.charAt(i);
if (counts.containsKey(tmp)) {
int count = (int)counts.get(tmp);
if (count <= 0)
return false;
else
counts.put(tmp, count - 1);
} else {
return false;
}
}
return true;
}
}
++x is called preincrement while x++ is called postincrement.
int x = 5, y = 5; System.out.println(++x); // outputs 6
System.out.println(x); // outputs 6 System.out.println(y++); // outputs 5
System.out.println(y); // outputs 6
Valid Anagram 解答的更多相关文章
- 【09_242】Valid Anagram
Valid Anagram My Submissions Question Total Accepted: 43694 Total Submissions: 111615 Difficulty: Ea ...
- leetcode面试准备:Valid Anagram
leetcode面试准备:Valid Anagram 1 题目 Given two strings s and t, write a function to determine if t is an ...
- 242. Valid Anagram(C++)
242. Valid Anagram Given two strings s and t, write a function to determine if t is an anagram of s. ...
- 22. leetcode 242. Valid Anagram(由颠倒字母顺序而构成的字)
22. 242. Valid Anagram(由颠倒字母顺序而构成的字) Given two strings s and t, write a function to determine if t i ...
- 【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验证变位词
Given two strings s and t , write a function to determine if t is an anagram of s. Example 1: Input: ...
- leetcdoe Valid Anagram
题目连接 https://leetcode.com/problems/valid-anagram/ Valid Anagram Description Given two strings s and ...
- 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
242. Valid Anagram Easy Given two strings s and t , write a function to determine if t is an anagram ...
随机推荐
- c++ 13
一.向量 ... 10.size/resize/clear/capacity/reserve 1)向量的大小可增可减,使向量大小改变的函数包括:resize/push_back/pop_back/cl ...
- 【剑指offer】面试题32:从1到n整数中1出现的次数
题目: 求出1~13的整数中1出现的次数,并算出100~1300的整数中1出现的次数?为此他特别数了一下1~13中包含1的数字有1.10.11.12.13因此共出现6次,但是对于后面问题他就没辙了.A ...
- windows多线程同步总结
1.多线程同步与多线程互斥的关系 其实这也是我一直困扰的问题,在这里我只是说说我的理解.我的理解是多线程互斥是针对于多线程资源而言的. 而多线程同步是针对于多线程时序问题.由于线程的并发性导致其运行时 ...
- OSCLI
- IDX爱定客 | 氪加
IDX爱定客 | 氪加 个性化定制鞋网站,在线定制只需三分钟
- 图论专题训练1-D(K步最短路,矩阵连乘)
题目链接 /* *题目大意: *求出从i到j,刚好经过k条边的最短路; * *矩阵乘法的应用之一(国家队论文): *矩阵乘法不满足交换律,矩阵乘法满足结合律; *给定一个有向图,问从A点恰好走k步(允 ...
- MediaInfo源代码分析 1:整体结构
MediaInfo 用来分析视频和音频文件的编码和内容信息,是一款是自由软件 (免费使用.免费获得源代码).之前编程的时候,都是直接调用它提供的Dll,这次突然来了兴趣,想研究一下它内部究竟是怎么实现 ...
- Oracle—RMAN备份(二)
在Oracle RMAN备份(一)中,对各种文件在RMAN中备份进行了说明, 一.备份集的复制 在RMAN 备份中,可以备份其自己的备份,即备份一个文件放在多个目录下,oralce支持最多备份四个. ...
- IE浏览器中hasLayout的介绍
haslayout是Windows Internet Explorer渲染引擎的一个内部组成部分.在InternetExplorer中,一个元素要么对自身的内容进行计算大小和组织,要么依赖于父元素来计 ...
- web.cofing(新手必看)
花了点时间整理了一下ASP.NET Web.config配置文件的基本使用方法.很适合新手参看,由于Web.config在使用很灵活,可以自定义一些节点.所以这里只介绍一些比较常用的节点. <? ...