题意:

  给出两个字符串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 (简单题)的更多相关文章

  1. [LeetCode] Valid Anagram 验证变位词

    Given two strings s and t, write a function to determine if t is an anagram of s. For example, s = & ...

  2. 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 ...

  3. LeetCode——Valid Anagram

    Description: Given two strings s and t, write a function to determine if t is an anagram of s. For e ...

  4. LeetCode() Valid Anagram 有问题!!!

    为什么第一个通过,第二个不行呢? class Solution { public: bool isAnagram(string s, string t) { if(s.size() != t.size ...

  5. 力扣485. 最大连续1的个数-C语言实现-简单题

    题目 [题目传送门] 给定一个二进制数组, 计算其中最大连续1的个数. 示例 1: 输入: [1,1,0,1,1,1] 输出: 3 解释: 开头的两位和最后的三位都是连续1,所以最大连续1的个数是 3 ...

  6. 这样leetcode简单题都更完了

    这样leetcode简单题都更完了,作为水题王的我开始要更新leetcode中等题和难题了,有些挖了很久的坑也将在在这个阶段一一揭晓,接下来的算法性更强,我就要开始分专题更新题目,而不是再以我的A题顺 ...

  7. leetcode面试准备:Valid Anagram

    leetcode面试准备:Valid Anagram 1 题目 Given two strings s and t, write a function to determine if t is an ...

  8. LeetCode 242. 有效的字母异位词(Valid Anagram)

    242. 有效的字母异位词 LeetCode242. Valid Anagram 题目描述 给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的一个字母异位词. 示例 1: 输入: s ...

  9. leetcode简单题6

    今天的华师 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, fro ...

随机推荐

  1. node安装和配置

    windows 环境 安装node node下载地址 下载后点击安装,默认下一步即可(安装路径可更改为d:盘) 检测PATH环境变量是否配置了Node.js 点击开始=>运行=>输入&qu ...

  2. LSI SAS 3008 Web配置操作

    配置 LSI SAS 3008 介绍LSISAS3008的配置操作. 4.1 登录CU界面 介绍登录LSISAS3008的CU配置界面的方法. 4.2 创建RAID 介绍在LSISAS3008扣卡上创 ...

  3. uboot——详解各目录下的文件作用

    uboot下载地址:http://ftp.denx.de/pub/u-boot/ 1.目录分布 2.目录结构变化: u-boot-2010.03及以前版本 ├── api                ...

  4. Android OpenGLES2.0(十七)——球形天空盒VR效果实现

    在3D游戏中通常都会用到天空盒,在3D引擎中也一般会存在天空盒组件,让开发者可以直接使用.那么天空盒是什么?天空盒又是如何实现的呢?本篇博客主要介绍如何在Android中利用OpenGLES绘制一个天 ...

  5. À peu près là 隐私政策

    隐私政策 本应用尊重并保护所有使用服务用户的个人隐私权.为了给您提供更准确.更有个性化的服务,本应用会按照本隐私权政策的规定使用和披露您的个人信息.但本应用将以高度的勤勉.审慎义务对待这些信息.除本隐 ...

  6. AT2672 Coins

    传送门 按理说想到转化问题之后就不难了吧,可是我还是不会写 一个很容易想到的转化就是差分,将银币数和铜币数都减去金币数,这样就转化为\(x+y+z\)个钱币选\(y\)个银币和\(z\)个铜币的最大数 ...

  7. python中enumerate、xrange、range

    enumerate可以给列表自动生成一列,默认从0开始,自动增长1,可以指定默认开始值 list_product = ["thinkpad","macbook" ...

  8. Django与Ajax,文件上传,ajax发送json数据,基于Ajax的文件上传,SweetAlert插件

    一.Django与Ajax AJAX准备知识:JSON 什么是 JSON ? JSON 指的是 JavaScript 对象表示法(JavaScript Object Notation) JSON 是轻 ...

  9. 解决spark-shell输出日志信息过多

    import org.apache.log4j.Logger import org.apache.log4j.Level Logger.getLogger("org").setLe ...

  10. spring boot之 Bean的初始化和销毁(4)

    原文:https://blog.csdn.net/z3133464733/article/details/79189699 -------------------------------------- ...