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 only lowercase alphabets

Follow up:
What if the inputs contain unicode characters? How would you adapt your solution to such case?

解答思路:

设置一个大小为26的整整型数组来记录s这个字符串中每个字母出现的次数,判断t字符串中每个字母出现的次数是否与之相同,若相同,则说明构成变位词。

代码如下:

class Solution {
public:
    bool isAnagram(string s, string t) {
        int s_size = s.length();
        int t_size = t.length();
        if(s_size != t_size)
        {
            return false;
        }
        if(s_size == 0)
            return true;
        char s_copy[s_size + 1];
        char t_copy[t_size + 1];
        strncpy(s_copy,s.c_str(),s_size);
        strncpy(t_copy,t.c_str(),t_size);
        int num[26]={0};
        for(int i = 0;i < s_size;i++)
        {
            num[s_copy[i]-‘a‘]++;
        }
        for(int i = 0;i < t_size;i++)
        {
            num[t_copy[i]-‘a‘]--;
            if(num[t_copy[i]-‘a‘] < 0)
                return false;
        }
        for(int i = 0;i < 26;i++)
        {
            if(num[i] != 0)
                return false;
        }
        return true;
    }
};

时间: 2024-11-01 14:39:01

Leetcode题目:Valid Anagram的相关文章

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

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】Valid Anagram

题目链接:https://leetcode.com/problems/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", r

[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]50. 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 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)统计

leetCode(53):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