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;
    }
};
相关推荐
程序员-King.3 分钟前
【ROS1】Noetic 开发工作流程与常用命令
c++·ubuntu·ros
云深麋鹿11 分钟前
C++ | 基于红黑树实现mymap与myset
开发语言·c++
傲世仙尊2 小时前
string增删查改-insert-erase-replace与find加substr组合技
c++
·present·3 小时前
CSAPP(lecture 3)
c++
别动我齐刘海4 小时前
机器人 C++ / 系统联调补强2——异常处理、Protobuf
linux·c++·人工智能·学习·算法·机器学习·机器人
basketball6164 小时前
C++ 中字符串与常见类型的转换总结
开发语言·c++
等我调个Bug4 小时前
C++智能指针通俗讲解
开发语言·c++
kyle~6 小时前
时间同步 --- PTP 与 Scheduled Action Command
c++·数码相机·机器人·传感器
syagain_zsx8 小时前
WebSocket 教程:websocketpp 写 echo 服务端
linux·网络·c++·websocket·网络协议·工具库