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. 学习重点:1、金典的设计模式在实际中应用2、JVM原理3、jui源代码

    学习重点:1.金典的设计模式在实际中应用 2.JVM原理 3.jui源代码

  2. 如何重写EF DBContext 获取链接字符串的方法

    public partial class byvarDBFirst: DbContext { //使用自定义连接串 private static string GetEFConnctionString ...

  3. /Users/XX/Library/Developer/Xcode/DerivedData/XX.app/xxsdk.bundle Directory not empty

    今天在升级xcode后真机调试偶然发现这个问题,查了一些资料发现还是不能完全解决 解决方法:参考的(http://blog.csdn.net/alincexiaohao/article/details ...

  4. IOS开发中针对UIImageView的几种常用手势

    // //  ViewController.m //  05-手势 // //  Created by wanghy on 15/9/21. //  Copyright (c) 2015年 wangh ...

  5. Java中多线程的使用!!

    简介:       1.要了解多线程,首先我们得先了解进程和线程.那么什么是进程?进程就是一个正在运行的程序分配内存让应用程序能够运行的叫做进程.那么什么又是线程呢?线程:在一个程序中,负责代码的执行 ...

  6. 04_过滤器Filter_02_Filter解决中文乱码问题

    [过滤器解决中文乱码问题实例] [工程截图] [web.xml] <?xml version="1.0" encoding="UTF-8"?> &l ...

  7. 常用命令su ls cp cd mv cat touch mkdir rm head less more pwd tac 等

    1.用户切换 su:switch user su kevin   //半切换,切换到kevin用户,但是不读取kevin用户的配置文件 su - kevin   //完全切换,执行这个命令的时候表示切 ...

  8. ES6笔记-正则表达式和字符串正则方法

    RegExp构造函数 在ES5中,RegExp构造函数的参数有两种情况. 第一种情况是,参数是字符串,这时第二个参数表示正则表达式的修饰符(flag). var regex = new RegExp( ...

  9. 网站开发常用jQuery插件总结(七)背景插件backstretch

    一.backstretch插件功能 优化网站外观.主要用于设置页面背景图片,也可以设置html元素的背景图片.背景图片可以设置多张,在间隔时间内循环显示. 注 但是在设置背景图片时,如果图片过大,网站 ...

  10. 回答了个问题,9x9 乘法表生成器

    # -*- coding: utf-8 -*- from prettytable import PrettyTable pt = PrettyTable() # 需要安装prettytable这个库来 ...