LeetCode OJ 之 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.

思路:

统计第一个字符串的每个字符出现的次数,再统计第二个字符串的每个字符出现的次数。然后比较,如果都相同则返回true。如果不同返回false。

代码1:

class Solution {
public:
    bool isAnagram(string s, string t)
    {
        int len1 = s.size();
        int len2 = t.size();
        if(len1 != len2)
            return false;
        int map[26] = {0};//由于题目确定每个字符都是小写字母,因此只需要申请26个空间即可
        for(int i = 0 ; i < len1 ; i++)
        {
            map[s[i]-'a']++;
        }
        for(int i = 0 ; i < len1 ; i++)
        {
            map[t[i]-'a']--;
            if(map[t[i]-'a'] < 0)
                return false;
        }
        return true;
    }
};

代码2:

class Solution {
public:
    bool isAnagram(string s, string t)
    {
        int len1 = s.size();
        int len2 = t.size();
        if(len1 != len2)
            return false;
        int map[26] = {0};//由于题目确定每个字符都是小写字母,因此只需要申请26个空间即可
        for(int i = 0 ; i < len1 ; i++)
        {
            map[s[i]-'a']++;
            map[t[i]-'a']--;
        }
        for(int i = 0 ; i < len1 ; i++)
        {
            if(map[t[i]-'a'] != 0)
                return false;
        }
        return true;
    }
};

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-11 21:39:33

LeetCode OJ 之 Valid Anagram的相关文章

【LeetCode】242. Valid Anagram (2 solutions)

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

LeetCode OJ:Valid Sudoku(有效数独问题)

Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be partially filled, where empty cells are filled with the character '.'. 注意这里的有效数独并非指的是可以解出来,只要存在的数满足数独的条件就可以了. 原理很简单,但是判定在同一个blocks的时候出了点问题,没想到判定方法,看了下

LeetCode OJ:Valid Parentheses(有效括号)

Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]"

[LeetCode] NO. 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. [题目解析] 首先明确题意,需要判断组成两个字符串的所有字符是否完全相同,即

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 contain

【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

【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

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. 翻译:判断给定的两个字符串是否为打乱了顺序的同一个字符串 我的

【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. 题意: 给2个字符串,判断他们是否相等,只是顺序不一样(专业术语叫:变位词). 思路: