leetcode面试准备:Valid Anagram

1 题目

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.

接口: public boolean isAnagram(String s, String t)

2 思路

题意

对比两个字符串是否是一样的。

HashMap思想存储字符,字符串s往map里面存储,字符串t往map里面取。最后看map是否为空:if 空,true.

复杂度: Time: O(n) , Space:O(26)

3 代码

    public boolean isAnagram(String s, String t) {
Map<Character, Integer> smap = new HashMap<Character, Integer>();
int slen = s.length();
int tlen = t.length();
if (slen != tlen)
return false;
for (int i = 0; i < slen; i++) {
Character c = s.charAt(i);
if (smap.containsKey(c)) {
int count = smap.get(c);
count++;
smap.put(c, count);
} else {
smap.put(c,1);
}
} for (int i = 0; i < tlen; i++) {
Character c = t.charAt(i);
if (smap.containsKey(c)) {
int count = smap.get(c);
count--;
if(count == 0) {
smap.remove(c);
} else {
smap.put(c, count);
}
} else {
return false;
}
}
return smap.isEmpty();
}

4 总结

HashMap思想。

leetcode面试准备:Valid Anagram的更多相关文章

  1. 【LeetCode】242. Valid Anagram (2 solutions)

    Valid Anagram Given two strings s and t, write a function to determine if t is an anagram of s. For ...

  2. 【一天一道LeetCode】#242. Valid Anagram

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given t ...

  3. LeetCode算法题-Valid Anagram(Java实现)

    这是悦乐书的第198次更新,第205篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第61题(顺位题号是242).给定两个字符串s和t,写一个函数来确定t是否是s的anag ...

  4. LeetCode OJ:Valid Anagram(有效字谜问题)

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

  5. 【LeetCode】242. Valid Anagram 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 字典统计词频 排序 日期 [LeetCode] 题目地址:ht ...

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

  7. LeetCode OJ 之 Valid Anagram

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

  8. LeetCode 242. 有效的字母异位词(Valid Anagram)

    242. 有效的字母异位词 LeetCode242. Valid Anagram 题目描述 给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的一个字母异位词. 示例 1: 输入: s ...

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

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

随机推荐

  1. 在Linux下开始C语言的学习

    为什么要在linux下学习C语言? linux下可以体验到最纯粹的C语言编程,可以抛出其他IDE的影响 环境配置简单,一条命令就足够.甚至对于大多数linux发行版本,都已经不需要配置C语言的环境 查 ...

  2. SQL Server内连接、外连接、交叉连接

    前言 在数据库查询中,我们常常会用到的表连接查询,而我自己在工作中也是时常用这些表连接查询.而就在刚刚我却还没有搞清楚数据库表连接到底有哪几种, 这几种表连接查询方式又有什么区别,实属惭愧!借以此文以 ...

  3. 阿里云ECS被攻击

    今天发现阿里云ECS被攻击了,记录一下, /1.1 Match1:{ :;};/usr/bin/perl -e 'print .content-type: text/plain.r.n.r.nxsuc ...

  4. Conversion to Dalvik format failed:Unable toexecute dex: method ID not in [0, 0xffff]: 65536

    关于方法数超限,Google官方给出的方案是这样的:https://developer.android.com/intl/zh-cn/tools/building/multidex.html 我也写过 ...

  5. C#常用正则验证

    #region Protected Property protected Regex rLetters { get { return new Regex("[a-zA-Z]{1,}" ...

  6. linux下神奇的script命令

    script 是一个神奇命令,script 能够将终端的会话过程录制下来,然后使用 scriptreplay 就可以将其录制的结果播放给他人观看.script 的好处就在于你在终端中的所有操作.敲过的 ...

  7. job还是job

    declare jobno binary_integer;rm_days number;rm_hour number;  --传入的hourmy_hour number;    --取出当前时间的ho ...

  8. eclipse+PyDev 中报错"scrapy.spiders.Spider" ,可用"# @UndefinedVariable"压制.

    # -*- coding:utf-8 -*- ''' Created on 2015年10月22日 (1.1) 例子来源: http://scrapy-chs.readthedocs.org/zh_C ...

  9. jsp a标签传值到action中,action接收不到传值

    因为需求,今天在action中加了一个marker属性,尝试了很多方法 set,get方法也生成了,但是就接收不到值. 这时我注意到action中有我之前使用ajax请求数据返回json格式数据,不以 ...

  10. java编程思想,对象导论

    程序设计的本质就是使用编程语言解决某一类具体问题.对问题的定义叫建模,例如定义问题域中的各种名词,动作,结果等.针对具体的问题提出的解决方案叫算法. 面向对象程序设计的挑战之一,就是在问题空间的元素和 ...