728x90
https://leetcode.com/problems/valid-anagram/
Valid Anagram - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
alpha 배열을 통해서 알파벳 순서에 맞춰서 개수를 세웠다.
애너그램은 알파벳의 개수와 종류만 같으면 된다!
bool isAnagram(string s, string t) {
bool answer = true;
int alphas[26];
int alphat[26];
for(int i=0; i<26; i++) {
alphas[i] = 0;
alphat[i] = 0;
}
for(int i=0; i<s.size(); i++) alphas[s[i]-'a']++;
for(int i=0; i<t.size(); i++) alphat[t[i]-'a']++;
for(int i=0; i<26; i++) if(alphat[i] != alphas[i]) return false;
return answer;
}
728x90
'알고리즘 > LeetCode' 카테고리의 다른 글
[LeetCode/easy] Reverse Vowels of a String (0) | 2022.08.12 |
---|---|
[LeetCode/easy] Word Pattern (0) | 2022.08.12 |
[LeetCode/easy] Isomorphic Strings (0) | 2022.08.12 |
[LeetCode/easy] Valid Palindrome (0) | 2022.08.12 |
[LeetCode/easy] Add Binary (0) | 2022.08.12 |