알고리즘/프로그래머스
프로그래머스 A로 B만들기
고구마와 감자
2022. 12. 10. 23:24
https://school.programmers.co.kr/learn/courses/30/lessons/120886
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
public int solution1(String before, String after) {
int answer = 0;
char[] arr = after.toCharArray();
int[] count1 = new int[arr.length];
int[] count2 = new int[arr.length];
for (int i = 0; i < before.length(); i++) {
char c = before.charAt(i);
char d = after.charAt(i);
for (int j = 0; j < arr.length; j++) {
if (c == arr[j]) count1[j]++;
if (d == arr[j]) count2[j]++;
}
}
return Arrays.equals(count1, count2) ? 1 : 0;
}
다른 풀이
public int solution(String before, String after) {
return isCheck(getList(before), getList(after)) ? 1 : 0;
}
private boolean isCheck(List<Integer> first, List<Integer> second) {
for (int i = 0; i < first.size(); i++) {
if (first.get(i) != second.get(i)) {
return false;
}
}
return true;
}
// 문자의 빈도만 리스트로 반환, 문자는 없음
private List<Integer> getList(String str) {
return Arrays.stream(str.split(""))
.collect(Collectors.groupingBy(s -> s))
.values()
.stream()
.map(List::size)
.collect(Collectors.toList());
}