C++ 242. 有效的字母异位词 (a b字符串计数判断)

给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的字母异位词。

注意:若 s 和 t 中每个字符出现的次数都相同,则称 s 和 t 互为字母异位词。

示例 1:

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

输出: true

示例 2:

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

输出: false

提示:

1 <= s.length, t.length <= 5 * 104

s 和 t 仅包含小写字母

进阶: 如果输入字符串包含 unicode 字符怎么办?你能否调整你的解法来应对这种情况?

cpp 复制代码
class Solution {
public:
    bool isAnagram(string s, string t) {
        int a[26] = {0}, b[26] = {0};
        for(auto x : s) a[x - 'a'] ++;
        for(auto x : t) b[x - 'a'] ++;
        for(int i = 0; i < 26; i ++) if(a[i] != b[i]) return false;
        return true;
    }
};
相关推荐
·present·19 小时前
CSAPP(lecture 4_01)
c语言·c++
FPGA小迷弟20 小时前
用 Claude Code 重构遗留系统:从评估到落地的完整实践指南
c++·python·ai·plotly·django·ai编程·claude
程序员-King.20 小时前
【ROS1】Noetic 开发工作流程与常用命令
c++·ubuntu·ros
云深麋鹿20 小时前
C++ | 基于红黑树实现mymap与myset
开发语言·c++
傲世仙尊1 天前
string增删查改-insert-erase-replace与find加substr组合技
c++
·present·1 天前
CSAPP(lecture 3)
c++
别动我齐刘海1 天前
机器人 C++ / 系统联调补强2——异常处理、Protobuf
linux·c++·人工智能·学习·算法·机器学习·机器人
basketball6161 天前
C++ 中字符串与常见类型的转换总结
开发语言·c++
等我调个Bug1 天前
C++智能指针通俗讲解
开发语言·c++
kyle~1 天前
时间同步 --- PTP 与 Scheduled Action Command
c++·数码相机·机器人·传感器