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++指针初始化总结
1.字符指针的初始化 在c语言中 "string"保存的就是首个字符所在的地址 所以可以把 字符串常量"string" 赋值给指针 char *p; &qu ...
- poj 2342 Anniversary party_经典树状dp
题意:Ural大学有n个职员,1~N编号,他们有从属关系,就是说他们关系就像一棵树,父节点就是子节点的直接上司,每个职员有一个快乐指数,现在要开会,职员和职员的直接上司不能同时开会,问怎才能使开会的快 ...
- C#中,表达式的计算遵循一个规律:从左到右依次计算。
int i = 0; int j = (i++)+(i++)=(i++)+i=i+++i=i+++i++=1;
- View中取设置了Tag的UILabel
UILabel *badge = (UILabel *)[self.view viewWithTag:];
- MP3文件结构及解码概述
MP3文件结构概述 Layer-3音频文件.MPEG(MovingPicture Experts Group)在汉语中译为活动图像专家组,特指活动影音压缩标准,MPEG音频文件是MPEG1标准中的声音 ...
- css2如何设置全屏背景图片
每次在做一个网站后台登陆页面的时候,当UI给我一张背景是不规律的背景图片,但是在设置为背景时,总会遇到屏幕大小的问题,导致背景图片有可能平铺.这样UI的效果达不到也会很难看. 本来我想用body{ba ...
- Socket介绍
一 socket协议 Socket协议的形象描述 socket的英文原义是“孔”或“插座”.在这里作为4BDS UNIX的进程通信机制,取后一种意思.socket非常类似于电话插座.以一个国家级电话网 ...
- String.format Tutorial
String format(String format, Object... args) The format specifiers for general, character, and numer ...
- 网络编程之UDP
UDP编程流程:一服务端:1.创建socket. DatagramSocket socket = new DatagramSocket(8878); 2.创建数据包,用来接收发送的数据包.byte[ ...
- (转) Overloads and templates
Overloaded functions In C++, two different functions can have the same name if their parameters are ...