LeetCode Valid Anagram (简单题)
题意:
给出两个字符串s和t,判断串t是否为s打乱后的串。
思路:
如果返回的是true,则两个串的长度必定相等,所有字符出现的次数一样。那么可以统计26个字母的次数来解决,复杂度O(n)。也可以排序后逐个比对,复杂度O(nlogn)。
第一种方法:
class Solution {
public:
bool isAnagram(string s,string t)
{
if(s.size()!=t.size()) return false;
int cnt[][]={};
for(int i=; i<s.size(); i++)
cnt[s[i]-'a'][]++;
for(int i=; i<t.size(); i++)
cnt[t[i]-'a'][]++;
for(int i=; i<; i++)
if(cnt[i][]^cnt[i][])
return false;
return true;
}
};
AC代码
class Solution {
public:
bool isAnagram(string s,string t)
{
if(s.size()!=t.size()) return false;
int cnt[]={};
for(int i=; i<s.size(); i++) cnt[s[i]-'a']++,cnt[t[i]-'a']--;
for(int i=; i<; i++) if(cnt[i]) return false;
return true;
}
};
AC代码
第二种方法:
class Solution {
public:
bool isAnagram(string s,string t)
{
if(s.size()!=t.size()) return false;
sort(s.begin(),s.end());
sort(t.begin(),t.end());
for(int i=; i<s.size(); i++)
if(s[i]^t[i]) return false;
return true;
}
};
AC代码
bool isAnagram(string s,string t)
{
sort(s.begin(),s.end());
sort(t.begin(),t.end());
return s==t;
}
AC代码
python3
class Solution(object):
def isAnagram(self, s, t):
"""
:type s: str
:type t: str
::rtype: bool
"""
listt=list(t)
for x in s:
try:
listt.remove(x)
except ValueError:
return False
return True if listt==[] else False
AC代码
class Solution(object):
def isAnagram(self, s, t):
"""
:type s: str
:type t: str
::rtype: bool
"""
dic1, dic2= {}, {}
for x in s:
dic1[x]=dic1.get(x,0)+1
for x in t:
dic2[x]=dic2.get(x,0)+1
return dic1==dic2
AC代码
class Solution(object):
def isAnagram(self, s, t):
"""
:type s: str
:type t: str
::rtype: bool
"""
cnt1, cnt2= [0]*26, [0]*26
for x in s:
cnt1[ord(x)-ord('a')]+=1
for x in t:
cnt2[ord(x)-ord('a')]+=1
return cnt1==cnt2
AC代码
class Solution(object):
def isAnagram(self, s, t):
"""
:type s: str
:type t: str
::rtype: bool
"""
return sorted(s)==sorted(t)
AC代码
LeetCode Valid Anagram (简单题)的更多相关文章
- [LeetCode] Valid Anagram 验证变位词
Given two strings s and t, write a function to determine if t is an anagram of s. For example, s = & ...
- Leetcode Valid Anagram
Given two strings s and t, write a function to determine if t is an anagram of s. For example,s = &q ...
- LeetCode——Valid Anagram
Description: Given two strings s and t, write a function to determine if t is an anagram of s. For e ...
- LeetCode() Valid Anagram 有问题!!!
为什么第一个通过,第二个不行呢? class Solution { public: bool isAnagram(string s, string t) { if(s.size() != t.size ...
- 力扣485. 最大连续1的个数-C语言实现-简单题
题目 [题目传送门] 给定一个二进制数组, 计算其中最大连续1的个数. 示例 1: 输入: [1,1,0,1,1,1] 输出: 3 解释: 开头的两位和最后的三位都是连续1,所以最大连续1的个数是 3 ...
- 这样leetcode简单题都更完了
这样leetcode简单题都更完了,作为水题王的我开始要更新leetcode中等题和难题了,有些挖了很久的坑也将在在这个阶段一一揭晓,接下来的算法性更强,我就要开始分专题更新题目,而不是再以我的A题顺 ...
- leetcode面试准备:Valid Anagram
leetcode面试准备:Valid Anagram 1 题目 Given two strings s and t, write a function to determine if t is an ...
- LeetCode 242. 有效的字母异位词(Valid Anagram)
242. 有效的字母异位词 LeetCode242. Valid Anagram 题目描述 给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的一个字母异位词. 示例 1: 输入: s ...
- leetcode简单题6
今天的华师 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, fro ...
随机推荐
- iscsi使用教程(下)
动态创建目标 安装iscsi服务端 # yum install -y scsi-target-utils.x86_64 修改配置文件 # mkdir -p ~/volumes # sed -i '$ ...
- nodebrew
创建: 2019/05/30 完成: 2019/05/30 安装 安装 curl -L git.io/nodebrew | perl - setup 更新nodebrew nodebrew sel ...
- 「CF744C」Hongcow Buys a Deck of Cards「状压 DP」
题意 你有\(n\)个物品,物品和硬币有\(A\),\(B\)两种类型,假设你有\(M\)个\(A\)物品和\(N\)个\(B\)物品 每一轮你可以选择获得\(A, B\)硬币各\(1\)个,或者(硬 ...
- 数据绑定—Source(绑定到静态类的静态属性)
<UserControl x:Class="绑定.绑定Source" xmlns="http://schemas.microsoft.com/winfx/2006/ ...
- Master 接受其它组件的注册
Master对其它组件注册的处理: Master接受注册的对象主要就是: Driver.Application.Worker.注意:Executor 不会向 Master 注册,它是向 Driver ...
- InfoQ —— 腾讯游戏大数据服务场景与应用
简介 周东祥,本人从2010年毕业进入腾讯互动娱乐部门工作,一直致力在腾讯游戏运营开发工作.先后负责SAP业务受理系统,盗号自助系统,元数据系统以及近2年在腾讯游戏大数据运营开发中积累大量的大数据开发 ...
- 动手写一个简单版的谷歌TPU-矩阵乘法和卷积
谷歌TPU是一个设计良好的矩阵计算加速单元,可以很好的加速神经网络的计算.本系列文章将利用公开的TPU V1相关资料,对其进行一定的简化.推测和修改,来实际编写一个简单版本的谷歌TPU.计划实现到行为 ...
- 基础篇 - pg_isready
pg_isready 发起一个到指定 PostgreSQL数据库的连接检查. 使用方法: pg_isready [选项]... 选项: -d, --dbname=DBNAME 数据库名 -q, --q ...
- AT2160 へんなコンパス / Manhattan Compass
传送门 乍一看像是一个计算几何,然后想到了BFS,但是苦于无奈\(O(n^2)\)不会优化 然后以下参考zjq_shadow大佬的思路 显然发现曼哈顿距离很麻烦,除了暴力枚举貌似没什么很好的办法 考虑 ...
- 分别使用http,express,koa请求第三方接口
今天又再次恶补了一下http的内容,确切地说是node.js里面的http的内容,啊,百度了半天express怎么请求第三方接口,结果发现自己买的入门书籍都有这个内容.舍近求远,我真是醉了.还有百度上 ...