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 ...
随机推荐
- Compiled Language vs Scripting Language
Referrence: Blog Compiled Languages Example: C, C++, Java Source code needs to be compiled into bits ...
- 第02讲- Android开发环境
第02讲Android开发环境 需要下载的软件: JDK(JavaDevelopment Kit) Eclipse AndroidSDK(SoftwareDevelopmentKit) ADT(And ...
- hdu 4336 Card Collector(期望 dp 状态压缩)
Problem Description In your childhood, people in the famous novel Water Margin, you will win an amaz ...
- 绩效等级系统与MBO
1. 目标管理(MBO)在一定时期内(一般为一年)组织活动的期望成果,是组织使命在一定时期内的具体化,是衡量组织活动有效性的标准.由于组织活动个体活动的有机叠加,因此只有各个员工.各个部门的工作对组织 ...
- Javascript 解构的用处
对象的解构赋值,可以很方便地将现有对象的方法,赋值到某个变量. let { log, sin, cos } = Math; 上面代码将Math对象的对数.正弦.余弦三个方法,赋值到对应的变量上,使用起 ...
- 我为什么放弃Go语言
有好几次,当我想起来的时候,总是会问自己:我为什么要放弃Go语言?这个决定是正确的吗?是明智和理性的吗?事实上我一直在认真思考这个问题. 开门见山地说,我当初放弃Go语言(golang),就是由于两个 ...
- 划分树 poj2104 hdu5249
KPI Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submis ...
- EffectiveC#9--明白几个相等运算之间的关系
1.当你创建你自己的类型时(不管是类还是结构),你要定义类型在什么情况下是相等的.C#提供了4个不同的方法来断定两个对象是否是相等的 public static bool ReferenceEqual ...
- 面试前的准备---C#知识点回顾----03
经过一天的奔波,喜忧参半,不细表 再回看下标题,C#知识点回顾 再看下内容,数据库3NF 原谅我这个标题党 今天继续回忆 1.HTTP中Post和Get区别 这忒简单了吧,大家是不是感觉到兴奋了,长舒 ...
- jquery selector
jquery的选择器功能 1 :lt(index) selector 一组元素选择index之前的元素,若index<0 则倒着选过来 http://api.jquery.com/lt-sele ...