【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数据科学-1
R数据科学(R for Data Science) Part 1:探索 by: PJX for 查漏补缺 exercise: https://jrnold.github.io/r4ds-exercis ...
- C语言中的各种字符串输入方法
C语言从stdin读取一行字符串的几种方法 gets gets函数的头文件是<stdio.h>,原型如下: char *gets(char *s); gets从stdin中读入一行内容到s ...
- 学习java的第二十八天
一.今日收获 1.java完全学习手册第三章算法的3.2排序,比较了跟c语言排序上的不同 2.观看哔哩哔哩上的教学视频 二.今日问题 1.快速排序法的运行调试多次 2.哔哩哔哩教学视频的一些术语不太理 ...
- Yarn 公平调度器案例
目录 公平调度器案例 需求 配置多队列的公平调度器 1 修改yarn-site.xml文件,加入以下从参数 2 配置fair-scheduler.xml 3 分发配置文件重启yarn 4 测试提交任务 ...
- admire, admit
admire 当别人admire你时,小心掉进泥潭(mire).词源:to wonder. wonderful夸人,awful骂人,awesome夸人.admiral与admire词源不同,碰巧长得像 ...
- ALitum技巧
创建异型焊盘的方法 SCH与PCB同步修改后元器件乱跑的解决方法 Altium 在PCB重新编号更新到SCH原理图的方法 同步问题 其他技巧: 当前层亮色,其他层灰色切换:SHIFT+S
- OpenStack之四: keystone验证服务(端口5000)
#官网地址:https://docs.openstack.org/keystone/stein/install/keystone-install-rdo.html #:创建库,并授权 MariaDB ...
- IDE搬进浏览器里——JetBrains Projector
发展 提起 JetBrains,你会想到什么?各路强大的 IDE,比如 Android Studio.IDEA.WebStorm--这些对于开发者来说耳熟能详的产品都出自这家公司,这些 IDE 的功能 ...
- MySQL(5):安装MySQL
下载地址 下载地址:https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.19-winx64.zip 安装步骤 第一步:下载得到压缩包,并解压 ...
- 【编程思想】【设计模式】【创建模式creational】原形模式Prototype
Python版 https://github.com/faif/python-patterns/blob/master/creational/prototype.py #!/usr/bin/env p ...