문제
https://school.programmers.co.kr/learn/courses/30/lessons/155652
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
접근 방법 및 풀이
skip에 포함된 문자인지 확인 하는 부분에서도 코드 값이 'z'를 넘어가는 경우 소문자 알파벳 범위를 넘어서지 않도록 주의
import java.util.HashSet;
import java.util.Set;
public class Q30_155652 {
public String solution(String s, String skip, int index) {
Set<Character> set = new HashSet<>();
int skipLength = skip.length();
for (int i = 0; i < skipLength; i++) {
set.add(skip.charAt(i));
}
StringBuilder sb = new StringBuilder();
int sLength = s.length();
for (int i = 0; i < sLength; i++) {
char c = s.charAt(i);
for (int j = 0; j < index; j++) {
c = this.shift(c, set);
}
sb.append(c);
}
return sb.toString();
}
private char shift(char c, Set<Character> skip) {
int shiftCount = 1;
while (skip.contains(this.normalize((int) c + shiftCount))) {
shiftCount++;
shiftCount %= 26;
}
return this.normalize((int) c + shiftCount);
}
private char normalize(int code) {
if (code > 122) {
code -= 26;
}
return (char) code;
}
}