알고리즘/LeetCode

[LeetCode/easy] Valid Anagram

녕이 2022. 8. 12. 18:13
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