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. 将商品SKU数据按商品分组,组装成json数据

    需要封装的数据   将这些数据,分组出来,OLGoodsID相同的为一组,然后每个组的OLSKUID,放在一个字段里,变成 [{"OLGoodID":"test06261 ...

  2. AJAX与servlet的信息交互

    <%@ page language="java" import="java.util.*" pageEncoding="gb2312" ...

  3. ASP.NET 设计模式(转)

    Professional ASP.NET Design Patterns 为什么学习设计模式? 运用到ASP.NET应用程序中的设计模式.原则和最佳实践.设计模式和原则支持松散耦合.高内聚的代码,而这 ...

  4. Centos6.5 64linux系统基础优化(二)

    1  操作的最小化原则 1)安装系统最小化 2)开启程序服务最小化原则 3)操作最小化原则 4)登陆最小化原则;平时没有需求不用root登陆,要用普通登陆. 2  更改ssh服务默认端口及常规配置 # ...

  5. 《Think in Java》读书笔记一:对象

    一.抽象过程 Alan Kay曾经总结了第一个成功的面向对象语言.同时也是Java所基于的语言之一的SmallTalk的五个基本特性,这些特性表现了一种纯粹的面向对象程序设计方式: 1.万物皆为对象. ...

  6. jdbc的封装

    package com.wjf.helper; import java.io.FileInputStream; import java.io.FileOutputStream; import java ...

  7. IDC机房动力环境设备维护

    高低压配电                                              空调                                               ...

  8. c++ primer复习(四)

    1 标准库容器 顺序容器:vector.list.deque 容器适配器:stack.queue.priority_queue 2 容器元素类型约束: 容器元素类型必须支持复制和赋值,因为容器存放的都 ...

  9. ASP.NET设计模式(一)、适配器模式、依赖注入依赖倒置、空对象模式

    鸟随凤鸾,人伴贤良,得以共之,我之幸也.说的是鸟随着鸾凤可以飞的更高远,人和比自己境界高的相处,自己也会得到熏染进步. 一.概述 分享出来简单的心得,望探讨 依赖倒置 依赖注入 Adapter模式 N ...

  10. 【原创】Linux 内核模块编程

    sudo gedit hello.c #include <linux/module.h> #include <linux/kernel.h> #include <linu ...