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, ...
随机推荐
- Linux Java开发坏境搭建,Ubuntu-jdk+tomcat+eclipse+svn 包安装详细操作
更新时间2015-03-15 更新2015-04-12 svn安装更新 第一步 安装jdk (在linux上使用yum安装JDK http://blog.chinaunix.net/uid-1546 ...
- JBOSS EAP 6 系列五 Managed domains 管理域最主要的功能是“统一部署,统一配置”
摘要 本文首先介绍Managed Domain的概念,管理域最主要的功能是"统一部署,统一配置".接下来通过一个实例在"统一配置"部分实现一个双机配置起来的域, ...
- shell的数值计算,小数计算
shell脚本中,可以进行数值计算, 如加减乘除,通过expr.let.(())等完成,文章介绍:http://blog.csdn.net/longshenlmj/article/details/14 ...
- Android开发学习之路--RxAndroid之初体验
学了一段时间android,看了部分的项目代码,然后想想老是学基础也够枯燥乏味的,那么就来学习学习新东西吧,相信很多学java的都听说过RxJava,那么android下也有RxAndroid. Rx ...
- 05 Activity 跳转传值
第一个Activity: package com.fmyboke; import java.io.Serializable; import java.util.ArrayList; import ja ...
- java中hashCode()与equals()详解
首先之所以会将hashCode()与equals()放到一起是因为它们具备一个相同的作用:用来比较某个东西.其中hashCode()主要是用在hash表中提高 查找效率,而equals()则相对而言使 ...
- Android简易实战教程--第四话《最简单的短信发送器》
首先配置一个布局: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmln ...
- 小文本——Cookies
http协议的无状态性导致在需要会话的场景下寸步难行,例如一个网站为了方便用户,在一段时间内登录过改网站的浏览器客户端实现自动登录,为实现这种客户端与服务器之间的会话机制需要额外的一些标识,http头 ...
- 【一天一道LeetCode】#237. Delete Node in a Linked List
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Write a ...
- java设计模式---三种工厂模式之间的区别
简单工厂,工厂方法,抽象工厂都属于设计模式中的创建型模式.其主要功能都是帮助我们把对象的实例化部分抽取了出来,优化了系统的架构,并且增强了系统的扩展性. 本文是本人对这三种模式学习后的一个小结以及对他 ...