leetcode 242. 有效的字母异位词(Valid Anagram)

题目描述:

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

示例 1:

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

示例 2:

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

说明:
你可以假设字符串只包含小写字母。

进阶:

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

解法:

class Solution {
public:
    bool isAnagram(string s, string t) {
        vector<int> count1(128, 0);
        vector<int> count2(128, 0);
        for(char ch : s){
            count1[ch]++;
        }
        for(char ch : t){
            count2[ch]++;
        }
        for(int i = 0; i < 128; i++){
            if(count1[i] != count2[i]){
                return false;
            }
        }
        return true;
    }
};
posted @ 2019-03-21 14:36  zhanzq1  阅读(122)  评论(0)    收藏  举报