【LeetCode】242. 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 only lowercase alphabets.

还是比较简单的。

class Solution {
public:
    bool isAnagram(string s, string t) {
        unordered_map<char, int> dicts;

        if (s.size() != t.size()) return false;

        for (int i = 0; i < s.size(); ++i){
            dicts[s[i]]++;
        }

        for (int i = 0; i < t.size(); ++i){
            if (dicts.find(t[i]) == dicts.end() || dicts[t[i]] == 0) return false;
            else
                dicts[t[i]]--;
        }

        return true;
    }
};
时间: 2024-11-05 21:44:19

【LeetCode】242. Valid Anagram 解题小结的相关文章

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",

[LeetCode]: 242: 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. 分析: 判别“anagram”的条件: 1. 两个字符完全相等,两数完全为空:

LeetCode 242 Valid Anagram(有效字谜)(*)

翻译 给定两个字符串s和t,写一个函数来确定是否t是s的字谜. 例如, s = "anagram", t = "nagaram", 返回true s = "rat", t = "car", 返回false 备注: 你可以假设字符串只包含小写字母. 跟进: 如果输入包含unicode字符该怎么做?你的解决方案能够适应这种情况吗? 原文 Given two strings s and t, write a function to

Leetcode 242 Valid Anagram pytyhon

题目: 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. 用到了python的sorted函数,原型:sorted(data, cmp=

[LeetCode] 242. Valid Anagram Java

题目: 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. 题意及分析:要求判断两个字符串是否由相同的字符组成.这里有两种方法,(1)统计

(easy)LeetCode 242.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. 解法:英文颠倒词,只要两个单词中,英文字母出现的次数相等即可. 代码如下: publi

Leetcode 242 Valid Anagram 字符串处理

字符串s和字符串t是否异构,就是统计两个字符串的a-z的字符数量是否一值 1 class Solution { 2 public: 3 4 bool isAnagram(string s, string t) { 5 int flgs[26] = {0};//统计s a-z的字符数量 6 for(int i = 0;i<s.size();++i){ 7 flgs[s[i] - 'a'] ++; 8 } 9 int flgt[26] = {0}; //统计t a-z的字符数量 10 for(int

[leetcode]242. Valid Anagram判断两个字符串是不是包含相同字符的重排列

/* 思路是判断26个字符在两个字符串中出现的次数是不是都一样,如果一样就返回true. 记住这个方法 */ if (s.length()!=t.length()) return false; int[] words = new int[26]; for (int i = 0; i < s.length(); i++) { words[s.charAt(i)-'a']++; words[t.charAt(i)-'a']--; } for (int i = 0; i < 26; i++) { i

242. Valid Anagram(C++)

242. 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. 题目大意: 判断两字符串含有的元素是否相同.