[LeetCode] 242 - Valid Anagram
문제
- 링크: https://leetcode.com/problems/valid-anagram
- 난이도: Easy
- 태그: 해시 테이블, 문자열, 정렬
- 결과:
Time: 0 ms (100%), Space: 9.62 MB (73.75%)
풀이
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?
이 경우에는 해시맵을 채택하면 된다.
댓글남기기