【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: ...
随机推荐
- [R] 添加误差棒的分组折线图:geom_path: Each group consists of only one observation. Do you need to adjust the...
想做一个简单的分组折线图,并添加误差棒,类似下面这样的: 用ggplot似乎很简单就能实现:ggplot+geom_errorbar+geom_line+geom_point,重点在于计算误差棒. 还 ...
- C语言计算fastq文件GC含量2
改进了一下,利用zlib可以读取gz格式的压缩文件,也可以直接计算非压缩格式 #include <stdio.h> #include <stdlib.h> #include & ...
- 搭建简单的SpringCloud项目二:服务层和消费层
GitHub:https://github.com/ownzyuan/test-cloud 前篇:搭建简单的SpringCloud项目一:注册中心和公共层 后篇:搭建简单的SpringCloud项目三 ...
- for no other reason than because
在狄更斯的<A Child History of England>中有段话: After some disputing among the priests, who said that a ...
- nodeJs-querystring 模块
JavaScript 标准参考教程(alpha) 草稿二:Node.js querystring 模块 GitHub TOP querystring 模块 来自<JavaScript 标准参考教 ...
- 最新的Android Sdk 使用Ant多渠道批量打包
实例工程.所需的文件都在最后的附件中. 今天花费了几个小时,参考网上的资料,期间遇到了好几个问题, 终于实现了使用Ant批量多渠道打包,现在,梳理一下思路,总结使用Ant批量多渠道打包的方法:1 ...
- python做一个http接口测试框架
目录结构 project case#测试用例 suite#测试目录 logs#测试日志 papi#测试类 result#测试结果 setting.py#配置文件 1.日志类,用于测试时日志记录 pya ...
- What happens when more restrictive access is given to a derived class method in C++?
We have discussed a similar topic in Java here. Unlike Java, C++ allows to give more restrictive acc ...
- MyBatis中sql实现时间查询的方法
<if test="startTime != null and startTime !=''"> AND lTime >= #{startTime} </i ...
- dbeaver可视化工具-连接clickhouse
下载地址 https://dbeaver.io/download/ 本次下载的是ZIP包,这种方式方便移动/备份软件 此软件可连接多种数据库,这里是连接clickhouse 新建连接 点击 上面的倒三 ...