09009

[프로그래머스 lv1] 둘만의 암호 본문

Algorithm/문자열
[프로그래머스 lv1] 둘만의 암호
09009

문제 보기

https://school.programmers.co.kr/learn/courses/30/lessons/155652

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

 

 

문제 해결

구한 인덱스가 배열 밖의 범위를 벗어날 경우를 생각하여 구한 인덱스의 길이를 원래 배열의 길이로 나눈 것으로 해결한다.

 

소스 코드

class Solution {
    public String solution(String s, String skip, int index) {
        
        String word = "abcdefghijklmnopqrstuvwxyz";
        StringBuilder sb = new StringBuilder();
        
        for (int i=0; i<skip.length(); i++) {
            word = word.replace(skip.substring(i, i+1), "");
        }
        
        for (int i=0; i<s.length(); i++) {
            int idx = (word.indexOf(s.charAt(i)) + index) % word.length();
            sb.append(word.substring(idx, idx+1));
        }
        
        return sb.toString();
    }
}