-
[코딩인터뷰완전분석] 1.2_순열확인_풀이알고리즘 2021. 8. 24. 22:46
Q. 문자열 두 개가 주어졌을 때 이 둘이 서로 순열 관계에 있는지 확인하는 메서드를 작성하라.
[체크]
대소문자는 구분해야 하는지
공백은 하나의 문자로 처리해야하는지
입력받은 두 문자열의 길이가 같은지 (널 체크도 하면 좋을것 같다.) 확인 후에 해당 문자열을 char배열로 만들어 정렬을 진행한다.
두 문자열을 모두 정렬했을시 값이 같으면 순열이므로 true 값을 반환하면 된다.
public String sort (String s) { char[] content = s.toCharArray(); java.util.Arrays.sort(content); return new String(content); } public boolean permutation (String s, String t) { if (s.length() != t.length()) return false; return sort(s).equals(sort(t)); }
'알고리즘' 카테고리의 다른 글
[leetcode] Third Maximum Number (0) 2021.09.03 [leetcode] Max Consecutive Ones II (0) 2021.09.03 [leetcode] Height Checker (0) 2021.08.24 [코딩인터뷰완전분석] 1.1_중복이 없는가_풀이 (0) 2021.08.24 알고리즘 학습법 (0) 2021.08.21