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 ...
随机推荐
- 整数划分——真正的递归经典例题(NYOJ——90)
先注明学习博客的地址:(http://www.cnblogs.com/hoodlum1980/archive/2008/10/11/1308493.html) 题目描述:任何正整数n都可以写成n=n1 ...
- C# Object reference not set to an instance of an object.
一.问题 Object reference not set to an instance of an object. (你调用的对象是空的) 二.解决问题 在使用 c# 的查询时,先筛选后在关联其他表 ...
- React 从入门到进阶之路(三)
之前的文章我们介绍了 React 创建组件.JSX 语法.绑定数据和绑定对象.接下来我们将介绍 React 绑定属性( 绑定class 绑定style).引入图片 循环数组渲染数据. 上一篇中我们 ...
- Codeforces#514E(贪心,并查集)
#include<bits/stdc++.h>using namespace std;long long w[100007],sum[100007];int fa[100007],degr ...
- PostFX v2后期处理特效包:升级更惊艳的视觉效果
https://mp.weixin.qq.com/s/BMkLLuagbhRSWspzeGhK7g Post-Processing Stack后期处理特效包能够轻松创建和调整高质量视觉效果,实现更为惊 ...
- 关于SqlDataReader使用的一点疑惑
C#中的SqlDataReader类(System.Data.SqlClient)是用来在保持打开数据库连接的状态下取数据用的 用法如下图: “保持与数据库的连接”这个特性也是SqlDataReade ...
- JD孔_20160901
1.买的 “[京东超市]GL格朗 耳温枪/电子体温计/温度计/耳温计EW-2” http://item.jd.com/385507.html 2.
- 01.Spring Ioc 容器
基本概念 Spring 的 Ioc 容器,通常也称应用上下文.它包含了两个概念 Ioc 和 容器: 容器:顾名思义就是用来装东西的,在 Spring 中容器里盛放的就是各种各样的 Bean.既然装了东 ...
- ubuntu网络已禁用解决办法
1.首先停掉network-manager这个服务 sudo service network-manager stop 2.删除掉NetworkManager.state这个文件 sudo rm /v ...
- Echarts同一页面多个图表自适应浏览器窗口大小——window.onresize
当前做的一个项目中,频繁使用到百度团队的Echarts,发在一个页面同时出现多个图表时,只有最后一个图表触发了window.onresize事件,查询官方文档后得到解决. 方法如下: hwChart. ...