LeetCode Valid Anagram (简单题)

题意:

  给出两个字符串s和t,判断串t是否为s打乱后的串。

思路:

  如果返回的是true,则两个串的长度必定相等,所有字符出现的次数一样。那么可以统计26个字母的次数来解决,复杂度O(n)。也可以排序后逐个比对,复杂度O(nlogn)。

第一种方法:

 1 class Solution {
 2 public:
 3     bool isAnagram(string s,string t)
 4     {
 5         if(s.size()!=t.size())    return false;
 6         int cnt[26][2]={0};
 7         for(int i=0; i<s.size(); i++)
 8             cnt[s[i]-‘a‘][0]++;
 9         for(int i=0; i<t.size(); i++)
10             cnt[t[i]-‘a‘][1]++;
11         for(int i=0; i<26; i++)
12             if(cnt[i][0]^cnt[i][1])
13                 return false;
14         return true;
15     }
16 };

AC代码

 1 class Solution {
 2 public:
 3     bool isAnagram(string s,string t)
 4     {
 5         if(s.size()!=t.size())    return false;
 6         int cnt[26]={0};
 7         for(int i=0; i<s.size(); i++)    cnt[s[i]-‘a‘]++,cnt[t[i]-‘a‘]--;
 8         for(int i=0; i<26; i++)    if(cnt[i])    return false;
 9         return true;
10     }
11 };

AC代码

第二种方法:

 1 class Solution {
 2 public:
 3     bool isAnagram(string s,string t)
 4     {
 5         if(s.size()!=t.size())    return false;
 6         sort(s.begin(),s.end());
 7         sort(t.begin(),t.end());
 8         for(int i=0; i<s.size(); i++)
 9             if(s[i]^t[i])    return false;
10         return true;
11     }
12 };

AC代码

1 bool isAnagram(string s,string t)
2 {
3     sort(s.begin(),s.end());
4     sort(t.begin(),t.end());
5     return s==t;
6 }

AC代码

时间: 2024-10-19 06:51:07

LeetCode Valid Anagram (简单题)的相关文章

Leetcode动态规划【简单题】

目录 Leetcode动态规划[简单题] 53. 最大子序和 题目描述 思路分析 复杂度分析 70.爬楼梯 题目描述 思路分析 复杂度分析 121.买卖股票的最佳时机 题目描述 思路分析 复杂度分析 303.区域和检索-数组不可变 题目描述 思路分析 复杂度分析 Leetcode动态规划[简单题] 动态规划(Dynamic programming,简称DP),是一种把原问题分解为相对简单的子问题的方式求解复杂问题的方法.动态规划相较于递归,拥有更少的计算量. 53. 最大子序和 题目描述 给定一

[leetcode] Valid Anagram 、 Find All Anagrams in a String

Valid Anagram Given two strings s and t , write a function to determine if t is an anagram of s. Example 1: Input: s = "anagram", t = "nagaram" Output: true Example 2: Input: s = "rat", t = "car" Output: false Note:

[LeetCode] Valid Anagram 验证变位词

Given two strings s and t, write a function to determine if t is an anagram of s. For example, s = "anagram", t = "nagaram", return true. s = "rat", t = "car", return false. Note: You may assume the string contains

(LeetCode)Valid Anagram --- 有效的混排字符串

Given two strings s and t, write a function to determine if t is an anagram of s. For example, s = "anagram", t = "nagaram", return true. s = "rat", t = "car", return false. Note: You may assume the string contains

[LeetCode] Valid Anagram

Given two strings s and t, write a function to determine if t is an anagram of s. For example,s = "anagram", t = "nagaram", return true.s = "rat", t = "car", return false. Note:You may assume the string contains onl

LeetCode——Valid Anagram

Description: Given two strings s and t, write a function to determine if t is an anagram of s. For example, s = "anagram", t = "nagaram", return true. s = "rat", t = "car", return false. Note: You may assume the str

[leetcode]Valid Anagram解题报告 C语言

[题目] Given two strings s and t, write a function to determine if t is an anagram of s. For example, s = "anagram", t = "nagaram", return true. s = "rat", t = "car", return false. Note: You may assume the string cont

LeetCode() Valid Anagram 有问题!!!

为什么第一个通过,第二个不行呢? class Solution { public: bool isAnagram(string s, string t) { if(s.size() != t.size()) return false; vector<int> coll(26,0); for(long i=0;i<s.size();i++) { coll[s[i]-'a']++; } for(long i=0;i<s.size();i++) { if(coll[t[i]-'a'] =

leetcode:242 Valid Anagram-每日编程第八题

Valid Anagram Total Accepted: 42673 Total Submissions: 109227 Difficulty: Easy Given two strings s and t, write a function to determine if t is an anagram of s. For example,s = "anagram", t = "nagaram", return true.s = "rat",