728x90
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
풀이
중복조합+PriorityQueue+Comparator를 사용해서 사전을 만들어줬다.
그리고 pq에서 하나씩 빼면서 찾아야 하는 word가 몇번째에 있는지 카운팅 해줬다!
조합/순열/중복조합/중복순열은 매번 풀때마다 헷갈리는 것 같다 ..
이부분 집중적으로 한번 풀어봐야지
전체코드
import java.io.*;
import java.util.*;
class Solution {
static PriorityQueue<String> pq = new PriorityQueue<>(new Comparator<>(){
public int compare(String o1, String o2){
return o1.compareTo(o2);
}
});
static int length;
static String result[];
static String arr[] = {"A", "E", "I", "O", "U"};
public int solution(String word) {
for(int i=1; i<=5; i++){
result = new String[i];
cal(0,0,i);
}
int cnt = 0;
while(!pq.isEmpty()){
String now = pq.poll();
cnt++;
if(now.equals(word)){
return cnt;
}else{
continue;
}
}
return cnt;
}
public void cal(int index, int start, int len){
if(index == len){
String tmp = "";
for(int i=0; i<len; i++){
tmp+=result[i];
}
pq.offer(tmp);
return;
}
for(int i=start; i<5; i++){
result[index] = arr[i];
cal(index+1, 0, len);
}
}
}
728x90
'코테 > Algorithm' 카테고리의 다른 글
[BOJ] 15652: N과 M(4) (JAVA) (0) | 2024.06.04 |
---|---|
[BOJ] 1141: 접두사 (JAVA) (1) | 2024.05.31 |
[BOJ] 20310: 타노스 (JAVA) (0) | 2024.05.23 |
[Programmers] 구명보트 (JAVA) (0) | 2024.05.22 |
[Programmers] 다음 큰 숫자 (JAVA) (0) | 2024.05.21 |