【LeetCode】242. Valid Anagram 解题报告(Java & Python)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
[LeetCode]
题目地址:https://leetcode.com/problems/valid-anagram/
Total Accepted: 78186 Total Submissions: 187211 Difficulty: Easy
题目描述
Given two strings s and t , write a function to determine if t is an anagram of s.
Example 1:
Input: s = "anagram", t = "nagaram"
Output: true
Example 2:
Input: s = "rat", t = "car"
Output: 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?
题目大意
字典统计词频
这个题刚开始我想的是通过s字符串的每个字符能否在t中找到的方式。这个显然是错的。没有考虑到多次出现的问题。即两字符串长度相等,而且每个字符出现的次数相同,那么就是变位词。
题目中说了只有小写,那就是只有26个字母。统计一下出现的频率就好了。两个字符串中如果出现的词的频率都一样则说明是变位词。
算法如下:
public class Solution {
public boolean isAnagram(String s, String t) {
if(s.length()!=t.length()){
return false;
}
int[] sTimes=new int[26];
int[] tTimes=new int[26];
for(int i=0;i<s.length();i++){
sTimes[s.charAt(i)-'a']+=1;
tTimes[t.charAt(i)-'a']+=1;
}
for(int i=0;i<sTimes.length;i++){
if(sTimes[i]!=tTimes[i]){
return false;
}
}
return true;
}
}
AC:12ms
在上面看到了用到了两个数组进行保存。这样空间复杂度比较大,可以用一个进行保存。
对于s, 将其作为字符数组进行遍历,在遍历的过程中,对每个出现的字符计数加一。
对于t, 同样将其遍历,对每个出现的字符计数减一。
如果s和t是anagram , 那么最后的charcount数组中所有字符的计数都应该是0, 否则就不是anagram。
简化之后的代码:
public class Solution {
public boolean isAnagram(String s, String t) {
if(s.length()!=t.length()){
return false;
}
int[] times=new int[26];
for(int i=0;i<s.length();i++){
times[s.charAt(i)-'a']+=1;
times[t.charAt(i)-'a']-=1;
}
for(int i=0;i<times.length;i++){
if(times[i]!=0){
return false;
}
}
return true;
}
}
AC:10ms
使用python做法,也是统计词频,代码如下:
class Solution(object):
def isAnagram(self, s, t):
"""
:type s: str
:type t: str
:rtype: bool
"""
scount = collections.Counter(s)
tcount = collections.Counter(t)
return scount == tcount
排序
判断两个数字排序之后的结果是否一样即可。
class Solution(object):
def isAnagram(self, s, t):
"""
:type s: str
:type t: str
:rtype: bool
"""
slist = list(s)
tlist = list(t)
return sorted(slist) == sorted(tlist)
日期
2016/4/30 13:08:21
2018 年 11 月 13 日 —— 时间有点快
【LeetCode】242. Valid Anagram 解题报告(Java & Python)的更多相关文章
- LeetCode 242 Valid Anagram 解题报告
题目要求 Given two strings s and t , write a function to determine if t is an anagram of s. 题目分析及思路 给出两个 ...
- 【LeetCode】36. Valid Sudoku 解题报告(Python)
[LeetCode]36. Valid Sudoku 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址 ...
- 【LeetCode】593. Valid Square 解题报告(Python)
[LeetCode]593. Valid Square 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...
- 【LeetCode】120. Triangle 解题报告(Python)
[LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...
- 22. leetcode 242. Valid Anagram(由颠倒字母顺序而构成的字)
22. 242. Valid Anagram(由颠倒字母顺序而构成的字) Given two strings s and t, write a function to determine if t i ...
- 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 ...
- 【LeetCode】383. Ransom Note 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 [LeetCo ...
- 【LeetCode】575. Distribute Candies 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...
- [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: ...
随机推荐
- Deep Learning(深度学习)整理,RNN,CNN,BP
申明:本文非笔者原创,原文转载自:http://www.sigvc.org/bbs/thread-2187-1-3.html 4.2.初级(浅层)特征表示 既然像素级的特征表示方法没有作用,那怎 ...
- 6 — springboot中设置默认首页 -没屁用
1.页面在static目录中时 2).测试 2.页面在templates模板引擎中时 1).这种需要导入相应的启动器 <dependency> <groupId>org.spr ...
- MapReduce04 框架原理Shuffle
目录 2 MapReduce工作流程 3 Shuffle机制(重点) 3.1 Shuffle机制 3.2 Partition分区 默认Partitioner分区 自定义Partitioner分区 自定 ...
- day06 目录结构
day06 目录结构 文件目录 /bin # 存放系统常用命令的目录 /boot # 系统引导程序+内核 /dev # 设备.光驱.硬盘 /etc # 存放系统或服务的配置文件 /home # 普通用 ...
- Celery进阶
Celery进阶 在你的应用中使用Celery 我们的项目 proj/__init__.py /celery.py /tasks.py 1 # celery.py 2 from celery ...
- Spark(三)【RDD中的自定义排序】
在RDD中默认的算子sortBy,sortByKey只能真的值类型数据升序或者降序 现需要对自定义对象进行自定义排序. 一组Person对象 /** * Person 样例类 * @param nam ...
- Windows 下 Node.js 开发环境搭建
1.利用CentOS Linux系统自带的yum命令安装.升级所需的程序库: sudo -s LANG=C yum -y install gcc gcc-c++ autoconf libjpeg li ...
- Java虚拟机(JVM)以及跨平台原理
相信大家已经了解到Java具有跨平台的特性,可以"一次编译,到处运行",在Windows下编写的程序,无需任何修改就可以在Linux下运行,这是C和C++很难做到的. 那么,跨平台 ...
- 【JAVA】【基础知识】Java程序执行过程
1. Java程序制作过程 使用文本编辑器进行编辑 2. 编译源文件,生成class文件(字节码文件) javac源文件路径. 3.运行程序class文件.
- java 多线程的状态迁移 常用线程方法分析
一.线程的各个状态 图中的线程状态(Thread.Stat 中定义的Enum 名)NEW.RUNNABLE .TERMINATED.WAITING.TIMED_WAITING 和BLOCKED 都能够 ...