题意:

  给出两个字符串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. adnroid 启动是没有标题栏

    <activity android:name=".MainActivity" android:theme="@android:style/Theme.Light.N ...

  2. ProtoBuf练习(二)

    重复数据类型 protobuf语言的重复字段类型相当于C++的std::list数据类型 工程目录结构 $ ls proto/ TServer.proto TSession.proto proto文件 ...

  3. hdu 5693 D Game

    D Game HDU - 5693 众所周知,度度熊喜欢的字符只有两个:B 和D. 今天,它发明了一个游戏:D游戏. 度度熊的英文并不是很高明,所以这里的D,没什么高深的含义,只是代指等差数列[(等差 ...

  4. 在mac上使用sublime text3搭建opencv3开发环境

    安装sublime text3 打开mac终端,安装brew 安装opencv3,终端输入下面的coomand: brew install opencv@3 注意:@3表示安装的版本,如果不加@3,那 ...

  5. jpanel使用布局管理器时,setsize会失效

    布局管理器会自动根据容器里面的控件大小自动调整size和位置 如果想设置容器的大小和位置,可以使用setPreferredSize方法.

  6. [SCOI2007]修车 费用流 BZOJ 1070

    题目描述 同一时刻有N位车主带着他们的爱车来到了汽车维修中心.维修中心共有M位技术人员,不同的技术人员对不同的车进行维修所用的时间是不同的.现在需要安排这M位技术人员所维修的车及顺序,使得顾客平均等待 ...

  7. HDU1863-畅通工程

    题目链接:点击打开链接 Problem Description 省政府"畅通工程"的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即 ...

  8. 消息中间件 | 消息协议 | STOPM -- 《分布式 消息中间件实践》笔记

    12年,STOMP1.2规范发布  简单的文本消息传输协议,提供一种可互相操作的连接格式,允许客户端与任意消息服务器进行交互 主要的概念     STOMP包含客户端和服务器,其中客户端指生产者或消费 ...

  9. codeforces round 472(DIV2)D Riverside Curio题解(思维题)

    题目传送门:http://codeforces.com/contest/957/problem/D 题意大致是这样的:有一个水池,每天都有一个水位(一个整数).每天都会在这一天的水位上划线(如果这个水 ...

  10. Luogu P2480 [SDOI2010]古代猪文 卢卡斯+组合+CRT

    好吧刚开始以为扩展卢卡斯然后就往上套..结果奇奇怪怪又WA又T...后来才意识到它的因子都是质数...qwq怕不是这就是学知识学傻了.. 题意:$ G^{\Sigma_{d|n} \space C_n ...