【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.

提示:

注意题目里提示了两个输入字符串中的字符均为小写字符,因此我们可以建立一个大小为26的字典(数组),其中的对应关系为{0:a, 1:b, ..., 25:z}。用它保存每一个字母出现的次数。

对s,将对应的数字+1,而对于t,将对应的数字-1。最后考察字典的所有元素是否都为零即可判断出每个字符出现的次数是否一致。

代码:

class Solution {
public:
    bool isAnagram(string s, string t) {
        if (s.size() != t.size()) return false;
        int dic[26] = {0};
        for (int i = 0; i < s.size(); ++i) {
            dic[s[i] - ‘a‘]++;
        }
        for (int i = 0; i < t.size(); ++i) {
            dic[t[i] - ‘a‘]--;
        }
        for (int i = 0; i < 26; ++i) {
            if (dic[i] != 0) return false;
        }
        return true;
    }
};
时间: 2024-10-13 11:50:02

【LeetCode】242. 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】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】Longest Valid Parentheses 解题报告

[题目] Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring is "()", which has length = 2. Another example

【LeetCode】36 - Valid Sudoku

Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.(http://sudoku.com.au/TheRules.aspx) The Sudoku board could be partially filled, where empty cells are filled with the character '.'. A partially filled sudoku which is valid. N

【leetcode】Longest Valid Parentheses

Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring is "()", which has length

【Leetcode】036. 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 '.'. A partially filled sudoku which is valid. Note:A valid Sudoku board (partia

[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】125 - Valid Palindrome

Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example,"A man, a plan, a canal: Panama" is a palindrome."race a car" is not a palindrome. Note:Have you consider that th

【LeetCode】20. Valid Parentheses

题目: 思路:用Stack基本操作完成. 要检测输入字符是否满足这个条件,一个非常合适的数据结构是stack,后进先出的特征正好满足检测的需求.在检测的时候,每次检查一个字符,如果是左括号,就入栈,如果是右括号,并且右括号和当前栈顶符号是左右配对的,那么就弹出栈顶并且进行下一次检测,如果不满足上面两种情况,就说明检查到了一个非法字符,返回false. public class Solution { public boolean isValid(String s) { if(s.length()=