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. Appium Python API 汇总(中文版)

    网络搜集而来,留着备用,方便自己也方便他人.感谢总结的人! 1.contexts contexts(self): Returns the contexts within the current ses ...

  2. Farseer.net轻量级开源框架 中级篇:执行SQL语句

    导航 目   录:Farseer.net轻量级开源框架 目录 上一篇:Farseer.net轻量级开源框架 中级篇: 事务的使用 下一篇:Farseer.net轻量级开源框架 中级篇: DbFacto ...

  3. Expectation-Maximization(EM) 算法

    Expectation-Maximization 算法是统计学中用来给带隐含变量的模型做最大似然(和最大后验概率)的一种方法.EM 的应用特别广泛,经典的比如做概率密度估计用的 Gaussian Mi ...

  4. 梦想CAD控件 2018.7.26更新

    下载地址: http://www.mxdraw.com/ndetail_107.html 1.  增加属性匹配功能 2.  增加List命令 3.  增加CAD图纸审图批注功能 4.  环形阵列功能 ...

  5. 第四节:EasyUI的一些操作

    一丶Datagrid //1.初始化页面数据 LoadGrid: function () { dgLog = $('#dg').datagrid({ url: '/Test_Areas/Test/St ...

  6. Linux内核-内存回收逻辑和算法(LRU)

    Linux内核内存回收逻辑和算法(LRU) LRU 链表 在 Linux 中,操作系统对 LRU 的实现主要是基于一对双向链表:active 链表和 inactive 链表,这两个链表是 Linux ...

  7. extract a page from a multiple pages pdf on Ubuntu OS

    extract a page from a multiple pages pdf 1 extract a page from a multiple pages pdf use pdftk packag ...

  8. 天猫双11红包前端jQuery

    [01]   浏览器支持:IE10+和其他现代浏览器.   效果图:       步骤:   HTML部分:   <div class="opacity" style=&qu ...

  9. 【Codeforces 159B】Matchmaker

    [链接] 我是链接,点我呀:) [题意] 笔和笔盖 笔有颜色和直径 笔盖也有颜色和直径 笔盖和笔如果直径相等 那么称为good 如果笔盖和笔直径相等.颜色也相等,那么称为very good 问你怎样安 ...

  10. 使用pycharm学习django纪实

    之前已经下了python3.7 首先官网下载pycharm专业版,然后利用学生邮箱激活 新建django项目,使用虚拟环境就好了,方便之后的服务器部署(大概 找到博客开始学习:https://blog ...