II Hash Table: Valid Anagram
2016-01-02 23:55
281 查看
1. Valid Anagram
given: two strings s and t, write a function to determine if t is an anagram of s.
e.g.
s="anagram", t="nagaram", return true.
s="rat", t="car", return false.
strategy: using java.util.Arrays.sort() method to sort the chars in the Strings
then compare two new sorted strings to see if they are equal. if it is
return true, otherwise return false.
public class Solution {
public boolean isAnagram(String s, String t) {
String x = sort(s);
String y = sort(t);
return (x.equals(y));
}
private static String sort(String a){
String original = a;
char[] chars = original.toCharArray();
Arrays.sort(chars);
String sorted = new String(chars);
return sorted;
}
}
given: two strings s and t, write a function to determine if t is an anagram of s.
e.g.
s="anagram", t="nagaram", return true.
s="rat", t="car", return false.
strategy: using java.util.Arrays.sort() method to sort the chars in the Strings
then compare two new sorted strings to see if they are equal. if it is
return true, otherwise return false.
public class Solution {
public boolean isAnagram(String s, String t) {
String x = sort(s);
String y = sort(t);
return (x.equals(y));
}
private static String sort(String a){
String original = a;
char[] chars = original.toCharArray();
Arrays.sort(chars);
String sorted = new String(chars);
return sorted;
}
}
相关文章推荐
- 多线程基础基础
- 黑马程序员——java基础之多线程
- Mac 系统下安装 IntelliJ IDEA
- 把工程部署在tomcat的root路径下
- 新年第二天,在csdn沉静下来
- 修改tomcat 启动45秒
- CPU访问内存
- Python sort and sorted
- leetcode 5:Longest Palindromic Substring 三种解法的java实现源代码,以及详细讲解
- 素数表
- 新年新气象
- 对称与非对称加密;SSL;HTTPS;AJP
- struts2 ,web.xml中配置为/*.action,运行报错Invalid <url-pattern> /*.action in filter mapp
- 链表之单链表约瑟夫问题(三)
- TmFunctions
- 解决undefine reference to '__aeabi_idiv' undefine reference to '__aeabi_idivmod'错误
- 【转】Eclipse上安装GIT插件EGit及使用
- voice lab2 – GK Operations
- TmDateUtil
- 《笨办法学Python》 第3课手记