Post

[프로그래머스] 둘 만의 암호 - Java

문제

둘 만의 암호


아이디어

  1. 각 문자에 대해 1씩 늘려가며 index 뒤 만큼에 해당하는 알파벳을 찾는다.
    이 때 +1한 알파벳이 skip에 들어있으면 카운트를 하지 않는다.

  2. index만큼 카운트 했으면 해당 알파벳으로 대체한다.


전체 코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Solution {
    public String solution(String s, String skip, int index) {
        StringBuilder answer = new StringBuilder();
        for (char c: s.toCharArray()) {
            int pos = c;
            int cnt = 0;
            while (cnt < index) {
                pos++;
                if (pos > 'z') {
                    pos = 'a';
                }
                if (!skip.contains(String.valueOf((char)pos))) {
                    cnt++;
                }
            }
            answer.append((char)pos);
        }
        return answer.toString();
    }
}


노트

바보같이 answer를 리스트로 놓고 문자열로 합치겠다고 컬렉터에 joining 쓰고 그랬다.

그냥 StringBuilder 쓰면 toString으로 즉결심판 할 수 있는건데 ㅡㅡ

This post is licensed under CC BY 4.0 by the author.