본문 바로가기
Algorithm

[leetCode 242번] Valid Anagram (Java)

by ddahu 2023. 8. 31.

1.문제

LeetCode - The World's Leading Online Programming Learning Platform

 

LeetCode - The World's Leading Online Programming Learning Platform

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


2.문제설명

주어진 문자열 s , t 를 가지고 애너그램인지 판별하는 문제이다

- 문자열 s 를 가지고 새로 조합하여 t 를 만들 수 있으면 애너그램이다.

 


3.소스코드

class Solution {
    public boolean isAnagram(String s, String t) {
        
        char[] arr1 = s.toCharArray();
        char[] arr2 = t.toCharArray();

        Arrays.sort(arr1);
        Arrays.sort(arr2);

     return Arrays.equals(arr1,arr2);
    }
    
}

4.소스코드설명

 

문자열을 배열로 만들어 정렬을 진행하여 정렬된 s 와 t가 같다면 조합 할 수 있는 조건이 충족된다.