LeetCode 面试经典150题 [41/150 有效的字母异位词]


avatar
GuoYulong 2024-06-26 45

题目描述

给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的字母异位词。
注意:若 s 和 t 中每个字符出现的次数都相同,则称 s 和 t 互为字母异位词。
示例 1:

输入: s = "anagram", t = "nagaram"
输出: true

示例 2:

输入: s = "rat", t = "car"
输出: false

个人C++解答

class Solution {
public:
    bool isAnagram(string s, string t) {
        if (s.size() != t.size())
            return false;
        int scount[26] = {0};
        int tcount[26] = {0};
        for (int i = 0; i < s.size(); i++) {
            scount[s[i] - 'a']++;
        }
        for (int i = 0; i < t.size(); i++) {
            tcount[t[i] - 'a']++;
        }
        for (int i = 0; i < 26; i++) {
            if (scount[i] != tcount[i])
                return false;
        }
        return true;
    }
};

相关阅读

注意!!!

新增会员中心页面,方便管理个人账户,充值功能暂不开启,请勿为本网站进行任何充值活动!!!

通知!!!

① 过年好!!!拖更几个月了已经,年后继续更新!!