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, ...
随机推荐
- [error]error while loading shared libraries: libpcre.so.1 解决
nginx 安装好之后,启动的时候报错 [root@localhost nginx-1.6.2]# /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin ...
- Struts 1 之配置文件
web.xml中配置Struts的入口Servlet--ActionServlet,ActionServlet不负责任何的业务处理,它只是查找Action名单,找到path属性与URL属性一致的Act ...
- Android加速度传感器
Android加速度传感器 效果图 手机平放桌面的两张截屏,数据一直在刷新 源码 下载地址(Android Studio工程):http://download.csdn.net/detail/q487 ...
- 保存图片到SD卡
添加SD卡写权限 方法 public void saveMyBitmap(String bitName, Bitmap mBitmap) { File f = new File("/sdca ...
- Android WebView选择本地文件上传
This sample demonstrate android webview choose file to upload. I just implement the client code ,the ...
- shell 数据流重定向操作符总结
最近看了鸟哥私房菜关于shell数据流重定向的内容,总结一下. 操作符: 1.标准输入(stdin):代码为0,符号:< 或者<< 2.标准输出(stdout):代码为1,符号:&g ...
- 07 设置View的显示与隐藏
在代码中: 例子: <span style="font-size:14px;">ImageButton imageButton = new ImageButton(th ...
- Android开发学习之路--Service之初体验
android最后一个组件便是service了,终于学习到最后一个组件了,从年前的开发环境的搭建,到现在学到最后一个组件花了三周的时间,期间记录的点点滴滴,照着书本学习编写的代码都受益匪浅,这里要感谢 ...
- Sublime Text 3 使用MarkDown编写带预览的文本
看到别人使用一个叫Markdown的标记语言来完成编码,心里就有点小激动,毕竟简短的几个符号,就可以写出如此精美的界面,实在是让人感到心旷神怡啊.于是我就在网上搜索了一些相关项的设置,于是便有了下面的 ...
- Uva - 512 - Spreadsheet Tracking
Data in spreadsheets are stored in cells, which are organized in rows (r) and columns (c). Some oper ...