본문 바로가기
백준 & 프로그래머스

프로그래머스. 둘만의 암호. Java and Python

by concho 2024. 1. 26.
import java.util.*;
class Solution {
    public String solution(String s, String skip, int index) {
        
        String answer = "";
        char[] skipChArr = skip.toCharArray();
        char[] sChArr = s.toCharArray();
        var skipChIntSet = new HashSet<Integer>();
        var chList = new ArrayList<Character>();
        
        for(char ch : skipChArr) skipChIntSet.add((int)ch);
        for(char ch : sChArr){
            int chInt = (int)ch;
            //index처리
            for(int i=0; i<index; i++){
                chInt++;
                if(chInt > (int)('z')) chInt = (int)('a');
                while(skipChIntSet.contains(chInt)){
                    chInt++;
                    if(chInt > (int)('z')) chInt = (int)('a');
                }
            }
            chList.add((char)chInt);
        }
        
        char[] chArrTm = new char[chList.size()];
        for(int i=0; i<chList.size(); i++) chArrTm[i] = chList.get(i);
        answer = String.valueOf(chArrTm);
        
        return answer;
    }
}

 

 

def solution(s, skip, index):
    answer, skipSet, reList, skipOrdSet = '', set(), [], set()
    for ch in skip:
        skipSet.add(ch)
        skipOrdSet.add(ord(ch))
        
    for ch in s:
        newAs = ord(ch)
        for i in range(index):
            newAs += 1
            if newAs > ord('z'):
                newAs = ord('a')
            while newAs in skipOrdSet:
                newAs += 1
                if newAs > ord('z'):
                    newAs = ord('a')
        reList.append(chr(newAs))
            
    answer = ''.join(reList) 
    return answer

댓글