문제

풀이

class Solution {
public:
    bool isAnagram(string s, string t) {
        if (s.size() != t.size())
        {
            return false;
        }

        int cnt_s[26];
        int cnt_t[26];
        
        int len = s.size();
        for (int i = 0; i < len; ++i)
        {
            cnt_s[s[i] - 'a']++;
            cnt_t[t[i] - 'a']++;
        }

        for (int i = 0; i < 26; ++i)
        {
            if(cnt_s[i] != cnt_t[i])
            {
                return false;
            }
        }

        return true;
    }
};

알파벳별로 개수를 세면 된다.

Follow up

What if the inputs contain Unicode characters? How would you adapt your solution to such a case?

이 경우에는 해시맵을 채택하면 된다.

이 포스트는 달레 스터디(2주차)에서 진행한 Blind 75 문제집 풀이의 일부입니다.

댓글남기기