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‘] == 0)
                return false;
            else
                coll[t[i]-‘a‘]--;
        }
        for(auto i:coll)
        {
            if(i != 0)
                return false;
        }
        return true;
    }
};

  

class Solution {
public:
    bool isAnagram(string s, string t) {
        if(s.size() != t.size())
            return false;
        vector<long> 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‘] == 0)
                return false;
            coll[t[i]-‘a‘]--;
        }
        for(int i=0;i<s.size();i++)
            if(coll[i] != 0)
                return false;
        return true;
    }
};

  

时间: 2024-12-28 14:38:53

LeetCode() Valid Anagram 有问题!!!的相关文章

[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 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 --- 有效的混排字符串

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解题报告 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 (简单题)

题意: 给出两个字符串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]

LeetCode:Valid Anagram

1.题目名称 Valid Anagram (易位构词) 2.题目地址 https://leetcode.com/problems/valid-anagram/ 3.题目内容 英文:Given two strings s and t, write a function to determine if t is an anagram of s. 中文:给出两个字符串,写一个函数判断t是否是s的易位构词 例如: s = "anagram", t = "nagaram",

[LeetCode][JavaScript]Valid Anagram

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 strin