알고리즘

Valid Anagram

e-pd 2020. 8. 25. 14:59

 

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

anagram은 숫서만 다른 문자를 말한다. dog <-> god

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution {
    public boolean isAnagram(String s, String t) {
         if (s == null || s == null || s.length() != t.length()) { return false;}
        char[] chars = s.toCharArray();
        char[] chars2 = t.toCharArray();
 
        Arrays.sort(chars);
        Arrays.sort(chars2);
 
        for (int i = 0; i < s.length(); i++) {
            if (chars[i] != chars2[i]) { return false;}
        }
        return true;
    }
}
cs

 

 

캐릭터 순서를 정렬을 해서 같은지 비교한다.