Leetocde_242_Valid Anagram
本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/48979767
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.
思路:
(1)题意为给定两个字符串,要求判定其中的一个字符串能否通过移位得到另一个字符串。
(2)该题考察的是两个字符串组成的字符是否完全一致。下面给出了三种不同的解题方法,方法一:将两个字符串转为字符数组,通过Arrays.sort()方法对字符数组进行排序,然后判断两字符数组组成的字符串是否完全一致来得到答案;方法二:借用map来保存其中一个字符串中的字符及其个数,然后遍历另一个字符串对应的字符数组,判定遍历到的字符是否存在map中,若不存在返回false,若存在,则当前字符在map中的值减1,遍历完即得到结果;方法三:借用一个整形数组来实现,该方法效率最好,且容易理解。由于a~Z对应的ASCII码值小于256,即创建一个256大小的数组即可,将其中一个字符串对应的字符存入数组中,数组下标为字符对应的ASCII码值,对应的值为当前字符的个数,然后遍历另一个字符串对应的字符数组,判断遍历得到的字符在整形数组中的值是否为0,若为0则返回false,否则将该字符对应的值减1,遍历完即得结果。
(3)详情见下方代码。希望本文对你有所帮助。
算法代码实现如下:
package leetcode;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
/**
*
* @author liqqc
*
*/
public class Valid_Anagram {
// use api method
public static boolean isAnagram(String s, String t) {
if (s == null || t == null)
return false;
if (s.trim().equals(t.trim()))
return true;
if (s.length() != t.length())
return false;
char[] charArray = s.toCharArray();
char[] charArray2 = t.toCharArray();
Arrays.sort(charArray);
Arrays.sort(charArray2);
return new String(charArray).equals(new String(charArray2));
}
// use map
public static boolean isAnagram2(String s, String t) {
if (s == null || t == null)
return false;
if (s.trim().equals(t.trim()))
return true;
if (s.length() != t.length())
return false;
char[] charArray = s.toCharArray();
char[] charArray2 = t.toCharArray();
Map<Character, Integer> map = new HashMap<Character, Integer>();
for (Character c : charArray) {
if (!map.containsKey(c)) {
map.put(c, 1);
} else {
map.put(c, map.get(c) + 1);
}
}
for (Character c : charArray2) {
if (!map.containsKey(c)) {
return false;
} else {
if (map.get(c) <= 0) {
return false;
} else {
map.put(c, map.get(c) - 1);
}
}
}
return true;
}
// use array
public static boolean isAnagram3(String s, String t) {
if (s == null || t == null)
return false;
if (s.length() != t.length())
return false;
int[] arr = new int[256];
for (char c : s.toCharArray()) {
if (arr[c] == 0) {
arr[c] = 1;
} else {
arr[c] = arr[c] + 1;
}
}
for (char c : t.toCharArray()) {
if (arr[c] == 0) {
return false;
} else {
arr[c] = arr[c] - 1;
}
}
return true;
}
}
Leetocde_242_Valid Anagram的更多相关文章
- [LeetCode] Valid Anagram 验证变位词
Given two strings s and t, write a function to determine if t is an anagram of s. For example, s = & ...
- Leetcode 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
Problem: Given two strings s and t, write a function to determine if t is an anagram of s. For examp ...
- 【09_242】Valid Anagram
Valid Anagram My Submissions Question Total Accepted: 43694 Total Submissions: 111615 Difficulty: Ea ...
- 【leetcode❤python】242. Valid Anagram
class Solution(object): def isAnagram(self, s, t): if sorted(list(s.lower()))==sorted(list ...
- 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 ...
- (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 ...
- 【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, ...
随机推荐
- Android开发基础规范(一)
转载请把头部出处链接和尾部二维码一起转载,本文出自逆流的鱼yuiop:http://blog.csdn.net/hejjunlin/article/details/52602487 前言:Androi ...
- springMVC源码分析--HandlerInterceptor拦截器(一)
对SpringMVC有所了解的人肯定接触过HandlerInterceptor拦截器,HandlerInterceptor接口给我们提供了3个方法: (1)preHandle: 在执行controll ...
- JAVA面向对象-----接口的特点
接口的特点 1.类实现接口可以通过implements实现,实现接口的时候必须把接口中的所有方法实现,一个类可以实现多个接口. 2.接口中定义的所有的属性默认是public static final的 ...
- ArrayList、HashMap、HashSet源码总结
ArrayList: 1. ArrayList是List接口的大小可变数组的实现,此实现是不同步的. 2. ArrayList内部使用类型为Object[]的数组存储元素. 3. ArrayList默 ...
- 1090. Highest Price in Supply Chain (25) -计层的BFS改进
题目如下: A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyon ...
- Java并发框架——公平性
所谓公平性指所有线程对临界资源申请访问权限的成功率都一样,不会让某些线程拥有优先权.通过前面的CLH Node FIFO学习知道了等待队列是一个先进先出的队列,那么是否就可以说每条线程获取锁时就是公平 ...
- 最简单的视频网站(JavaEE+FFmpeg)
本文记录一个最简单的视频网站系统.此前做过一些基于JavaEE中的SSH (Strut2 + Spring + Hibernate)的网站系统,但是一直没有做过一个视频网站系统,所以就打算做一个&qu ...
- 1082. Read Number in Chinese (25)
题目如下: Given an integer with no more than 9 digits, you are supposed to read it in the traditional Ch ...
- python类:属性
http://blog.csdn.net/pipisorry/article/details/50708616 Python 中的 property 属性 Python中有个很赞的概念,叫做prope ...
- Mybatis执行SimpleExecutor(三)
SimpleExecutor通过类名可以看出,它是一个简单的执行类,并不会做一些处理就执行sql,源码及分析如下: /** * @author Clinton Begin */ public clas ...