题意:

  给出两个字符串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. sqlserver2012——游标

    游标:一种数据访问机制,允许用户访问单独的数据行而不是对整个行集进行操作.用户可以通过单独处理每一行逐条收集信息并对数据逐行进行操作,这样可以将降低系统开销. 游标主要有以下两部分: 游标结果集:由定 ...

  2. Ubuntu16.4下安装Chrome浏览器以及Chromedriver

    一.Chrome浏览器的安装 对于谷歌Chrome32位版本,使用如下链接: wget https://dl.google.com/linux/direct/google-chrome-stable_ ...

  3. ASP.NET MVC中的 _ViewStart.cshtml文件的作用【摘抄】

    ViewStart 在前面的例子中,每一个视图都是使用Layout 属性来指定它的布局.如果多个视图使用同一个布局,就会产生冗余,并且很难维护. _ViewStart.cshtml 页面可用来消除这种 ...

  4. window 环境下在虚拟机上安装php环境

    转发:https://www.cnblogs.com/orangegem/p/7191659.html 安装linux工具 :https://blog.csdn.net/z15732621582/ar ...

  5. vue -- 父子组件间的事件触发

    1.父组件触发子组件事件 Parent.vue <child ref="child"></child> <div @click="fn&qu ...

  6. 2014-11-3 NOIP模拟赛2

    NOIP 2014 水题模拟赛 (请选手务必仔细阅读本页内容) 一.题目概况 中文题目名称 数列 刷漆 排队 英文题目与子目录名 seq paint layout 可执行文件名 seq paint l ...

  7. 帝都Day6——图论

    //P2O5呢? 一.图的存储: 邻接矩阵:邻接表. 邻接矩阵:n*n的[][],[i][j]节点有边记1没边0 缺点 空间复杂度O(n^2) 占用内存较大(我为什么要把这些东西写到这里呢???) 邻 ...

  8. Linux调优(文件系统)

    查看单个文件是否发生碎片化(被存在磁盘非连续磁盘块上) # filefrag -v /var/log/messages 查看文件系统是否存在大量碎片(会显示空闲离散的块) # dumpe2fs /de ...

  9. 微信小程序-修改单选框和复选框大小的方法

    方法有两种: 一:采用css的zoom属性 zoom缩放会将元素保持在左上角,并且会有毛边,可能会稍稍改变元素原来的形状. 二:采用css3的transform:scale属性 zoom缩放会将元素保 ...

  10. 关于node中的global,箭头函数的this的一个小问题

    this一直是一个JS中的困扰问题,这次在跑JS精粹的代码的时候顺带发现了Node里面全局变量的问题 var x = 1; var myObj = { x: 2 }; myObj.func = fun ...