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.

Note:
You may assume the string contains only lowercase alphabets.

Follow up:
What if the inputs contain unicode characters? How would you adapt your solution to such case?


题目标签:Hash Table

  题目给了我们两个string, 让我们判断它们是不是变位词。

  方法1:可以利用HashMap 来记录string s 的字母,和数量,接着用t 的字母和数量 来验证。

  方法2:可以把两个string 变为 char array,接着sort 两个array,比较它们是不是一致。

Java Solution 1:

Runtime beats 18.65%

完成日期:05/27/2017

关键词:HashMap

关键点:利用HashMap来存入s,用t 来验证

 class Solution
{
public boolean isAnagram(String s, String t)
{
/* Solution 1: HashMap */
HashMap<Character, Integer> map = new HashMap<>(); // first time: store each s char and occurrence into map
for(int i=0; i<s.length(); i++)
{
char sChar = s.charAt(i);
map.put(sChar, map.getOrDefault(sChar, 0) + 1);
}
// second time: compare t char with map to see match or not
for(int i=0; i<t.length(); i++)
{
char tChar = t.charAt(i); if(!map.containsKey(tChar))
return false; if(map.get(tChar) == 1)
map.remove(tChar);
else
map.put(tChar, map.get(tChar) - 1); } return map.size() == 0 ? true : false;
}
}

Java Solution 2:

Runtime beats 28.32%

完成日期:05/27/2017

关键词:Sort

关键点:把s 和t 都转化为char array,然后sort

 class Solution
{
public boolean isAnagram(String s, String t)
{
/* Solution 2: sort */
if(s.length() != t.length() || s == null || t == null)
return false; char[] s_arr = s.toCharArray(); Arrays.sort(s_arr); char[] t_arr = t.toCharArray(); Arrays.sort(t_arr); for(int i=0; i<s.length(); i++)
{
if(s_arr[i] != t_arr[i])
return false;
} return true; }
}

参考资料:N/A

LeetCode 题目列表 - LeetCode Questions List

LeetCode 242. Valid Anagram (验证变位词)的更多相关文章

  1. [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: ...

  2. [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: ...

  3. [LeetCode] Valid Anagram 验证变位词

    Given two strings s and t, write a function to determine if t is an anagram of s. For example, s = & ...

  4. 22. leetcode 242. Valid Anagram(由颠倒字母顺序而构成的字)

    22. 242. Valid Anagram(由颠倒字母顺序而构成的字) Given two strings s and t, write a function to determine if t i ...

  5. 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 ...

  6. 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 = & ...

  7. 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 ...

  8. (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 ...

  9. 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, ...

随机推荐

  1. Burp Suite抓https数据包

    本地环境JDK1.8Burp Suite 1.7.26 Firefox 59.0.2 一.burp介绍请自行谷歌,这里不过多介绍 二.配置HTTPS抓包方法[以Firefox为例]通常情况下burp默 ...

  2. Farseer.net轻量级开源框架 入门篇:逻辑层的选择

    导航 目   录:Farseer.net轻量级开源框架 目录 上一篇:Farseer.net轻量级开源框架 入门篇: 入门篇:增.删.改.查操作演示 下一篇:Farseer.net轻量级开源框架 入门 ...

  3. PHP7中session_start 使用注意事项,会导致浏览器刷时页面数据不更新

    //PHP7中session_start 使用注意事项, session_start([ 'cache_limiter' => 'private', //在读取完毕会话数据之后马上关闭会话存储文 ...

  4. 【sqli-labs】【jsp/tomcat】 less29 less30 less31 less32 (GET型利用HTTP参数污染的注入)

    sqli-labs带了几个Java版本的web注入,在tomcat-files.zip里 以Less29为例,查看源码,可以看出请求最后还是提交给了php应用,难怪less29文件夹下有一个没有任何防 ...

  5. Vue2.0 —生命周期和钩子函数

    vue生命周期简介 咱们从上图可以很明显的看出现在vue2.0都包括了哪些生命周期的函数了. 生命周期探究 对于执行顺序和什么时候执行,看上面两个图基本有个了解了.下面我们将结合代码去看看钩子函数的执 ...

  6. win10 javac无效

    win10配置环境变量时,要写绝对路径,不再需要写JAVA_HOME和classpaht,直接在pass上添加全路径就可以了.

  7. extjs动态插入一列

    StdDayWordQuery:function(btn,event){ var form=Ext.getCmp('queryFormSDW'); var userNameORuserCode = f ...

  8. 洛谷——P3807 【模板】卢卡斯定理

    P3807 [模板]卢卡斯定理 洛谷智推模板题,qwq,还是太弱啦,组合数基础模板题还没做过... 给定n,m,p($1\le n,m,p\le 10^5$) 求 $C_{n+m}^{m}\ mod\ ...

  9. jsonview插件的常见使用方法整理

    Jsonview是目前最热门的一款开发者工具插件,确切的来说jQuery JSONView是一款非常实用的格式化和语法高亮JSON格式数据查看器jQuery插件.它是查看json数据的神器. 下载地址 ...

  10. Java 字符串总结

    三种字符串类:String,StringBuilder,StringBuffer String类 1. 常用构造器 构造器   public String(char value[])   public ...