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. vs2017 visual studio2017 密钥 激活码

    企业版Enterprise: NJVYC-BMHX2-G77MM-4XJMR-6Q8QF 专业版Professional: KBJFW-NXHK6-W4WJM-CRMQB-G3CDH

  2. 从输入URL到网页呈现的过程

    1.域名解析当我们在浏览器中输入一个URL,例如”www.google.com”时,这个地址并不是谷歌网站真正意义上的地址.互联网上每一台计算机的唯一标识是它的IP地址,因此我们输入的网址首先需要先解 ...

  3. CAD使用GetxDataString读数据(网页版)

    主要用到函数说明: MxDrawEntity::GetxDataString2 读取一个字符扩展数据,详细说明如下: 参数 说明 [in] LONG lItem 该值所在位置 [out, retval ...

  4. ThinkPHP---AR模式

    [前言] 在之前学习框架时介绍过 (1)什么是框架? ①框架是一堆包含了常量.方法和类等代码集合: ②半成品应用,只包含了项目开发时的底层架构,并不包含业务逻辑: ③包含一些设计模式,例如单例模式,工 ...

  5. ThinkPHP---thinkphp视图(V)

    配置文件分3类:系统配置文件,分组配置文件,应用配置文件 ①系统配置文件ThinkPHP/Conf/convention.php: ②分组 / 模块 /平台配置文件Home/Conf/config.p ...

  6. linux网络编程——域名转换 gethostbyname与gethostbyaddr

    域名转换 #include <netdb.h> struct hostent *gethostbyname(const char *name); 参数: name: 执行主机名的指针 返回 ...

  7. 作业 3-5 switch语句的应用

    /*输入五级制成绩(A-E),输出相应的百分制成绩(0-100)区间*/ #include<stdio.h> int main(void) { char ch;/*定义一个字符*/ pri ...

  8. 微信小程序,获取点击元素的索引值index

    1.需求说明 点击 “加号图片” 上传图片,需要知道点击的是第几个图片,动态的修改src数组,这里图片用的 wx:for 循环出来的 2.遇到问题 按照官方最新文档循环的方式,索引值是以  wx:fo ...

  9. 在线安全清空慢查询日志slowlog

      mysql> show variables like '%slow_query%';+------------------------------------+--------------- ...

  10. Python基础—面向对象(初级篇)

    一.什么是面向对象编程 面向对象编程(Object Oriented Programming,OOP,面向对象程序设计),python语言比较灵活即支持面向对象编程也支持面向函数式编程. 面向过程编程 ...