Light Blue Pointer
본문 바로가기
Coding Test

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

by Greedy 2023. 12. 15.

문제 주소

 

프로그래머스

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

programmers.co.kr

문제 설명

두 문자열 s와 skip, 그리고 자연수 index가 주어질 때, 다음 규칙에 따라 문자열을 만들려 합니다. 암호의 규칙은 다음과 같습니다.

  • 문자열 s의 각 알파벳을 index만큼 뒤의 알파벳으로 바꿔줍니다.
  • index만큼의 뒤의 알파벳이 z를 넘어갈 경우 다시 a로 돌아갑니다.
  • skip에 있는 알파벳은 제외하고 건너뜁니다.

예를 들어 s = "aukks", skip = "wbqd", index = 5일 때, a에서 5만큼 뒤에 있는 알파벳은 f지만 [b, c, d, e, f]에서 'b'와 'd'는 skip에 포함되므로 세지 않습니다. 따라서 'b', 'd'를 제외하고 'a'에서 5만큼 뒤에 있는 알파벳은 [c, e, f, g, h] 순서에 의해 'h'가 됩니다. 나머지 "ukks" 또한 위 규칙대로 바꾸면 "appy"가 되며 결과는 "happy"가 됩니다.

두 문자열 s와 skip, 그리고 자연수 index가 매개변수로 주어질 때 위 규칙대로 s를 변환한 결과를 return하도록 solution 함수를 완성해주세요.


제한사항

  • 5 ≤ s의 길이 ≤ 50
  • 1 ≤ skip의 길이 ≤ 10
  • s와 skip은 알파벳 소문자로만 이루어져 있습니다.
    • skip에 포함되는 알파벳은 s에 포함되지 않습니다.
  • 1 ≤ index ≤ 20

입출력 예

s skip index result

"aukks" "wbqd" 5 "happy"

입출력 예 설명

입출력 예 #1

본문 내용과 일치합니다.

 

풀이 과정

HashMap 에 skip등록해봄 아무 이유없이...

한자 한자 미루면서 skip에 있는지 보면 될듯

 

a-z까지 index로 더해서 HashMap에 value에 넣어놨다가 나중에 읽어오는게 더 빠를거같음 ㅋㅋ?

아마 그럴듯…

ascii code

https://www.ascii-code.com/97 141 61 01100001 a

https://www.ascii-code.com/122 172 7A 01111010 z

122-97=25

import java.util.HashMap;
class Solution {
    public String solution(String s, String skip, int index) {
        
        HashMap<String,String> replace = new HashMap<String,String>();
        
        for(int i = 0; i<skip.length(); i++){
            String str = ""+skip.charAt(i);
            replace.put(str,str);
        }
        for(char c = 'a'; c<='z';c++){
            if(!replace.containsKey(""+c)){
                int delay = 0;
                char c2 = c;
                while(c2<=index+c){
                    if(replace.containsKey(""+(c2))){
                        delay++;
                        break;
                    }
                }
                if(c2+index+delay>122){
                    c2 = c2+index+delay-25;
                }else{
                    c2 = c2+index+delay;
                }
                replace.put(""+c,""+c2);
            }
        }
        
        String answer = "";
        for(int i=0; i<s.length(); i++){
            answer = answer + replace.get(""+s.charAt(i));
        }
        
        return answer;
    }
}

/Solution.java:22: error: incompatible types: possible lossy conversion from int to char

c2 = c2+index+delay-25;

^

/Solution.java:24: error: incompatible types: possible lossy conversion from int to char

c2 = c2+index+delay;

→ (char) 시켜줌

import java.util.HashMap;
class Solution {
    public String solution(String s, String skip, int index) {
        
        HashMap<String,String> replace = new HashMap<String,String>();
        
        for(int i = 0; i<skip.length(); i++){
            String str = ""+skip.charAt(i);
            replace.put(str,str);
        }
        for(char c = 'a'; c<='z';c++){
            if(!replace.containsKey(""+c)){
                int delay = 0;
                char c2 = c;
                while(c2<=index+c){
                    if(replace.containsKey(""+(c2))){
                        delay++;
                        break;
                    }
                }
                if(c2+index+delay>122){
                    c2 = (char)(c2+index+delay-25);
                }else{
                    c2 = (char)(c2+index+delay);
                }
                replace.put(""+c,""+c2);
            }
        }
        
        String answer = "";
        for(int i=0; i<s.length(); i++){
            answer = answer + replace.get(""+s.charAt(i));
        }
        
        return answer;
    }
}
실행 시간이 10.0초를 초과하여 실행이 중단되었습니다. 실행 시간이 더 짧은 다른 방법을 찾아보세요.
테스트 결과 (~˘▾˘)~
1개 중 0개 성공

→ while문 안 나왔나?

while문 안에 index를 ++를 안해주고 있었음…

추가해봄

import java.util.HashMap;
class Solution {
    public String solution(String s, String skip, int index) {
        
        HashMap<String,String> replace = new HashMap<String,String>();
        
        for(int i = 0; i<skip.length(); i++){
            String str = ""+skip.charAt(i);
            replace.put(str,str);
        }
        for(char c = 'a'; c<='z';c++){
            if(!replace.containsKey(""+c)){
                int delay = 0;
                char c2 = c;
                while(c2<=index+c){
                    if(replace.containsKey(""+(c2))){
                        delay++;
                    }
                    c2++;
                }
                if(c2+index+delay>122){
                    c2 = (char)(c2+index+delay-25);
                }else{
                    c2 = (char)(c2+index+delay);
                }
                replace.put(""+c,""+c2);
            }
        }
        
        String answer = "";
        for(int i=0; i<s.length(); i++){
            answer = answer + replace.get(""+s.charAt(i));
        }
        
        return answer;
    }
}
테스트 1
입력값 〉	"aukks", "wbqd", 5
기댓값 〉	"happy"
실행 결과 〉	실행한 결괏값 "nhvvf"이 기댓값 "happy"과 다릅니다.

a -> 97 
u -> 117
k -> 107
k -> 107
s -> 115

n -> 110 ->-6해야
h -> 104 -> -7해야
v -> 118 -> -6해야
v -> 118
f -> 102 -> -7해야

h -> 104
a -> 97
p -> 112
p -> 112
y -> 121

로그 찍어봄

import java.util.HashMap;
class Solution {
    public String solution(String s, String skip, int index) {
        
        HashMap<String,String> replace = new HashMap<String,String>();
        
        for(int i = 0; i<skip.length(); i++){
            String str = ""+skip.charAt(i);
            replace.put(str,str);
        }
        for(char c = 'a'; c<='z';c++){
            if(!replace.containsKey(""+c)){
                int delay = 0;
                char c2 = c;
                while(c2<=index+c){
                    if(replace.containsKey(""+(c2))){
                        delay++;
                    }
                    c2++;
                }
                if(c2+index+delay>122){
                    c2 = (char)(c2+index+delay-25);
                }else{
                    c2 = (char)(c2+index+delay);
                }
                replace.put(""+c,""+c2);
                System.out.println( c +" -> "+c2);
            }
        }
        
        String answer = "";
        for(int i=0; i<s.length(); i++){
            answer = answer + replace.get(""+s.charAt(i));
        }
        
        return answer;
    }
}
테스트 1
입력값 〉	"aukks", "wbqd", 5
기댓값 〉	"happy"
실행 결과 〉	실행한 결괏값 "nhvvf"이 기댓값 "happy"과 다릅니다.
출력 〉	
a -> n 110 h 104
c -> o 111 
e -> p 112
f -> q 113
g -> r 114
h -> s 115
i -> t 116
j -> u 117
k -> v 118 ->w는 훌륭하게 뛰어넘음
l -> x 120
m -> y 121
n -> z 122
o -> b 98 -> 97이 나와야되는데?
p -> c
r -> e
s -> f
t -> g
u -> h
v -> i
x -> j
y -> k
z -> l

일단 -26으로 바꾸니까 z후에 a가 나오긴 함 근데 b는 왜 안 건너뛰어질까?

import java.util.HashMap;
class Solution {
    public String solution(String s, String skip, int index) {
        
        HashMap<String,String> replace = new HashMap<String,String>();
        
        for(int i = 0; i<skip.length(); i++){
            String str = ""+skip.charAt(i);
            replace.put(str,str);
        }
        for(char c = 'a'; c<='z';c++){
            if(!replace.containsKey(""+c)){
                int delay = 0;
                char c2 = c;
                while(c2<=index+c){
                    if(replace.containsKey(""+(c2))){
                        delay++;
                    }
                    c2++;
                }
                if((int)c2+index+delay>122){
                    c2 = (char)((int)c2+index+delay-26);
                }else{
                    c2 = (char)((int)c2+index+delay);
                }
                replace.put(""+c,""+c2);
                System.out.println( c +" -> "+c2);
            }
        }
        
        String answer = "";
        for(int i=0; i<s.length(); i++){
            answer = answer + replace.get(""+s.charAt(i));
        }
        
        return answer;
    }
}
a -> n
c -> o
e -> p
f -> q
g -> r
h -> s
i -> t
j -> u
k -> v
l -> x
m -> y
n -> z
o -> a
p -> b
r -> d
s -> e
t -> f
u -> g
v -> h
x -> i
y -> j
z -> k

생각해보니 지금 b가 skip되어야 하는데 a로 넘어간 이후에는 skip이 안 되고 있음…

지금보니 index까지 ++로 c2를 증가시키는데 왜 index를 더 더해주는지 모르겠어서 뺌

import java.util.HashMap;
class Solution {
    public String solution(String s, String skip, int index) {
        
        HashMap<String,String> replace = new HashMap<String,String>();
        
        for(int i = 0; i<skip.length(); i++){
            replace.put(Character.toString(skip.charAt(i)),Character.toString(skip.charAt(i)));
        }
        for(char c = 'a'; c<='z';c++){
            if(!replace.containsKey(""+c)){
                int delay = 0;
                char c2 = c;
                int i = 0;
                while(i<=index){
                    if(replace.containsKey(Character.toString(c2))){
                        delay++;
                    }
                    c2++;
                    
                    if((int)c2+delay>122){
                        c2 = (char)((int)c2-26);
                    }
                    i++;
                }
                c2 = (char)((int)c2+delay);
                replace.put(Character.toString(c),Character.toString(c2));
                System.out.println( c +" -> "+c2);
            }
        }
        
        String answer = "";
        for(int i=0; i<s.length(); i++){
            answer = answer + replace.get(Character.toString(s.charAt(i)));
        }
        
        return answer;
    }
}
테스트 1
입력값 〉	"aukks", "wbqd", 5
기댓값 〉	"happy"
실행 결과 〉	실행한 결괏값 "ibqqz"이 기댓값 "happy"과 다릅니다.
출력 〉	a -> i
c -> j
e -> k
f -> l
g -> m
h -> n
i -> o
j -> p
k -> q
l -> s
m -> t
n -> u
o -> v
p -> w
r -> y
s -> z
t -> a
u -> b -> 왜 나오냐고요
v -> d
x -> g
y -> i
z -> k

a는 왜 안 나오고 b는 왜 나오냐고요

index 를 ≤가 아니라 <로 바꿔봄

테스트케이스 통과함

import java.util.HashMap;
class Solution {
    public String solution(String s, String skip, int index) {
        
        HashMap<String,String> replace = new HashMap<String,String>();
        
        for(int i = 0; i<skip.length(); i++){
            replace.put(Character.toString(skip.charAt(i)),Character.toString(skip.charAt(i)));
        }
        for(char c = 'a'; c<='z';c++){
            if(!replace.containsKey(""+c)){
                int delay = 0;
                char c2 = c;
                int i = 0;
                while(i<index){
                    if(replace.containsKey(Character.toString(c2))){
                        delay++;
                    }
                    c2++;
                    
                    if((int)c2+delay>122){
                        c2 = (char)((int)c2-26);
                    }
                    i++;
                }
                c2 = (char)((int)c2+delay);
                replace.put(Character.toString(c),Character.toString(c2));
                System.out.println( c +" -> "+c2);
            }
        }
        
        String answer = "";
        for(int i=0; i<s.length(); i++){
            answer = answer + replace.get(Character.toString(s.charAt(i)));
        }
        
        return answer;
    }
}

그리고 제출했더니 수많은 실패가

import java.util.HashMap;
class Solution {
    public String solution(String s, String skip, int index) {
        
        HashMap<String,String> replace = new HashMap<String,String>();
        HashMap<String,String> skips = new HashMap<String,String>();
        
        for(int i = 0; i<skip.length(); i++){
            skips.put(Character.toString(skip.charAt(i)),Character.toString(skip.charAt(i)));
        }
        for(char c = 'a'; c<='z';c++){
            if(!replace.containsKey(""+c)){
                int delay = 0;
                char c2 = c;
                int i = 0;
                while(i<=index){
                    if(skips.containsKey(Character.toString(c2))){
                        c2++;
                    }
                    c2++;
                    
                    if((int)c2>122){
                        c2 = (char)((int)c2-26);
                    }
                    i++;
                }
                c2 = (char)((int)c2);
                replace.put(Character.toString(c),Character.toString(c2));
                System.out.println( c +" -> "+c2);
            }
        }
        
        String answer = "";
        for(int i=0; i<s.length(); i++){
            answer = answer + replace.get(Character.toString(s.charAt(i)));
        }
        
        return answer;
    }
}

delay없애봄

테스트 1
입력값 〉	"aukks", "wbqd", 5
기댓값 〉	"happy"
실행 결과 〉	실행한 결괏값 "ibqqz"이 기댓값 "happy"과 다릅니다.
출력 〉	a -> i
b -> j
c -> j
d -> k
e -> k
f -> l
g -> m
h -> n
i -> o
j -> p
k -> q
l -> s
m -> t
n -> u
o -> v
p -> w
q -> y
r -> y
s -> z
t -> a
u -> b
v -> d
w -> f
x -> f
y -> g
z -> h

지금 생각해보니 그냥 c2++으로 1 증가시키고 끝일게 아니라 skips에 안 들어있을 때까지 읽어줘야 할 거 같아

i 를 index+1까지 하게 고쳐봄

import java.util.HashMap;
class Solution {
    public String solution(String s, String skip, int index) {
        
        HashMap<String,String> replace = new HashMap<String,String>();
        HashMap<String,String> skips = new HashMap<String,String>();
        
        for(int i = 0; i<skip.length(); i++){
            skips.put(Character.toString(skip.charAt(i)),Character.toString(skip.charAt(i)));
        }
        for(char c = 'a'; c<='z';c++){
            if(!skips.containsKey(Character.toString(c))){
                char c2 = c;
                int i = 0;
                while(i<=index+1){
                    if(!skips.containsKey(Character.toString(c2))){
                        i++;
                    }
                    c2++;
                    if((int)c2>122){
                        c2 = (char)((int)c2-26);
                    }
                }
                c2 = (char)((int)c2);
                replace.put(Character.toString(c),Character.toString(c2));
                System.out.println( c +" -> "+c2);
            }
        }
        
        String answer = "";
        for(int i=0; i<s.length(); i++){
            answer = answer + replace.get(Character.toString(s.charAt(i)));
        }
        
        return answer;
    }
}
테스트 1
입력값 〉	"aukks", "wbqd", 5
기댓값 〉	"happy"
실행 결과 〉	실행한 결괏값 "jdssa"이 기댓값 "happy"과 다릅니다.
출력 〉	
a -> j
c -> k
e -> l
f -> m
g -> n
h -> o
i -> p
j -> q
k -> s
l -> t
m -> u
n -> v
o -> w
p -> y
r -> z
s -> a
t -> b -> b가 나오면 안됨
u -> d
v -> f
x -> g
y -> h
z -> i

중복은 없어짐

b이면 못 나가고 한번 더 c2++하게 조건을 추가해봤더니 완전 망가짐

import java.util.HashMap;
class Solution {
    public String solution(String s, String skip, int index) {
        
        HashMap<String,String> replace = new HashMap<String,String>();
        HashMap<String,String> skips = new HashMap<String,String>();
        
        for(int i = 0; i<skip.length(); i++){
            skips.put(Character.toString(skip.charAt(i)),Character.toString(skip.charAt(i)));
        }
        for(char c = 'a'; c<='z';c++){
            if(!skips.containsKey(Character.toString(c))){
                char c2 = c;
                int i = 0;
                while(i<=index+1&&(!skips.containsKey(Character.toString(c2)))){
                    if(!skips.containsKey(Character.toString(c2))){
                        i++;
                    }
                    c2++;
                    if((int)c2>122){
                        c2 = (char)((int)c2-26);
                    }
                }
                c2 = (char)((int)c2);
                replace.put(Character.toString(c),Character.toString(c2));
                System.out.println( c +" -> "+c2);
            }
        }
        
        String answer = "";
        for(int i=0; i<s.length(); i++){
            answer = answer + replace.get(Character.toString(s.charAt(i)));
        }
        
        return answer;
    }
}
테스트 1
입력값 〉	"aukks", "wbqd", 5
기댓값 〉	"happy"
실행 결과 〉	실행한 결괏값 "bwqqw"이 기댓값 "happy"과 다릅니다.
출력 〉	
a -> b
c -> d
e -> l
f -> m
g -> n
h -> o
i -> p
j -> q
k -> q
l -> q
m -> q
n -> q
o -> q
p -> q
r -> w
s -> w
t -> w
u -> w
v -> w
x -> b
y -> b
z -> b

 

import java.util.HashMap;
class Solution {
    public String solution(String s, String skip, int index) {
        
        HashMap<String,String> replace = new HashMap<String,String>();
        HashMap<String,String> skips = new HashMap<String,String>();
        
        for(int i = 0; i<skip.length(); i++){
            skips.put(Character.toString(skip.charAt(i)),"0");
        }
        
        for(char c = 'a'; c<='z';c++){
            if(!skips.containsKey(Character.toString(c))){
                char c2 = c;
                int i = 0;
                while((i<=index+1)&&(!skips.containsKey(Character.toString(c2)))){
                    
                    if(!skips.containsKey(Character.toString(c2))){
                           i++;
                    }
                    
                    c2++;
                    if((int)c2>122){
                        c2 = (char)((int)c2-26);
                    }
                }
                c2 = (char)((int)c2);
                replace.put(Character.toString(c),Character.toString(c2));
                System.out.println( c +" -> "+c2);
            }
        }
        
        String answer = "";
        for(int i=0; i<s.length(); i++){
            answer = answer + replace.get(Character.toString(s.charAt(i)));
        }
        
        return answer;
    }
}
테스트 1
입력값 〉	"aukks", "wbqd", 5
기댓값 〉	"happy"
실행 결과 〉	실행한 결괏값 "bwqqw"이 기댓값 "happy"과 다릅니다.
출력 〉	
a -> b
c -> d
e -> l
f -> m
g -> n
h -> o
i -> p
j -> q
k -> q
l -> q
m -> q
n -> q
o -> q
p -> q
r -> w
s -> w
t -> w
u -> w
v -> w
x -> b
y -> b
z -> b

코드만 읽어서는 내가 잘 한거 같아서 로그 찍어봄

import java.util.HashMap;
class Solution {
    public String solution(String s, String skip, int index) {
        
        HashMap<String,String> replace = new HashMap<String,String>();
        HashMap<String,String> skips = new HashMap<String,String>();
        
        for(int i = 0; i<skip.length(); i++){
            skips.put(Character.toString(skip.charAt(i)),"0");
        }
        
        for(char c = 'a'; c<='z';c++){
            if(!skips.containsKey(Character.toString(c))){
                char c2 = c;
                System.out.println( "c2 " +c2);
                int i = 0;
                while((i<=index+1)&&(!skips.containsKey(Character.toString(c2)))){
                    if(!skips.containsKey(Character.toString(c2))){
                           i++;
                        System.out.println( "index" +i);
                    }
                    
                    c2++;
                    if((int)c2>122){
                        c2 = (char)((int)c2-26);
                    }
                }
                replace.put(Character.toString(c),Character.toString(c2));
                System.out.println( c +" -> "+c2);
            }
        }
        
        String answer = "";
        for(int i=0; i<s.length(); i++){
            answer = answer + replace.get(Character.toString(s.charAt(i)));
        }
        
        return answer;
    }
}
테스트 1
입력값 〉	"aukks", "wbqd", 5
기댓값 〉	"happy"
실행 결과 〉	실행한 결괏값 "bwqqw"이 기댓값 "happy"과 다릅니다.
출력 〉	
c2 a
index1 -> 여기서 끝나시면 안되는데 skips에 b가 있어버리니까 while문 탈출하나봄.....
a -> b
c2 c

나가는 조건을 &&로 주면 안되나봄

바꿔봄

import java.util.HashMap;
class Solution {
    public String solution(String s, String skip, int index) {
        
        HashMap<String,String> replace = new HashMap<String,String>();
        HashMap<String,String> skips = new HashMap<String,String>();
        
        for(int i = 0; i<skip.length(); i++){
            skips.put(Character.toString(skip.charAt(i)),"0");
        }
        
        for(char c = 'a'; c<='z';c++){
            if(!skips.containsKey(Character.toString(c))){
                char c2 = c;
                System.out.println( "c2 " +c2);
                int i = 0;
                while(true){
                    if(i>=index){
                        if(!skips.containsKey(Character.toString(c2))){
                            break;
                        }
                    }
                    if(!skips.containsKey(Character.toString(c2))){
                           i++;
                        System.out.println( "index" +i);
                    }
                    
                    c2++;
                    if((int)c2>122){
                        c2 = (char)((int)c2-26);
                    }
                }              
                
                
                
                replace.put(Character.toString(c),Character.toString(c2));
                System.out.println( c +" -> "+c2);
            }
        }
        
        String answer = "";
        for(int i=0; i<s.length(); i++){
            answer = answer + replace.get(Character.toString(s.charAt(i)));
        }
        
        return answer;
    }
}
테스트 1
입력값 〉	"aukks", "wbqd", 5
기댓값 〉	"happy"
실행 결과 〉	테스트를 통과하였습니다.
출력 〉	c2 a
index1
index2
index3
index4
index5
a -> h
c2 c
index1
index2
index3
index4
index5
c -> i
c2 e
index1
index2
index3
index4
index5
e -> j
c2 f
index1
index2
index3
index4
index5
f -> k
c2 g
index1
index2
index3
index4
index5
g -> l
c2 h
index1
index2
index3
index4
index5
h -> m
c2 i
index1
index2
index3
index4
index5
i -> n
c2 j
index1
index2
index3
index4
index5
j -> o
c2 k
index1
index2
index3
index4
index5
k -> p
c2 l
index1
index2
index3
index4
index5
l -> r
c2 m
index1
index2
index3
index4
index5
m -> s
c2 n
index1
index2
index3
index4
index5
n -> t
c2 o
index1
index2
index3
index4
index5
o -> u
c2 p
index1
index2
index3
index4
index5
p -> v
c2 r
index1
index2
index3
index4
index5
r -> x
c2 s
index1
index2
index3
index4
index5
s -> y
c2 t
index1
index2
index3
index4
index5
t -> z
c2 u
index1
index2
index3
index4
index5
u -> a
c2 v
index1
index2
index3
index4
index5
v -> c
c2 x
index1
index2
index3
index4
index5
x -> e
c2 y
index1
index2
index3
index4
index5
y -> f
c2 z
index1
index2
index3
index4
index5
z -> g

오오 통과됐다!!

 

 

제출 코드

import java.util.HashMap;
class Solution {
    public String solution(String s, String skip, int index) {
        
        HashMap<String,String> replace = new HashMap<String,String>();
        HashMap<String,String> skips = new HashMap<String,String>();
        
        for(int i = 0; i<skip.length(); i++){
            skips.put(Character.toString(skip.charAt(i)),"0");
        }
        
        for(char c = 'a'; c<='z';c++){
            if(!skips.containsKey(Character.toString(c))){
                char c2 = c;
                int i = 0;
                while(true){
                    if(i>=index){
                        if(!skips.containsKey(Character.toString(c2))){
                            break;
                        }
                    }
                    if(!skips.containsKey(Character.toString(c2))){
                           i++;
                    }
                    
                    c2++;
                    if((int)c2>122){
                        c2 = (char)((int)c2-26);
                    }
                }              
  
                replace.put(Character.toString(c),Character.toString(c2));
                System.out.println( c +" -> "+c2);
            }
        }
        
        String answer = "";
        for(int i=0; i<s.length(); i++){
            answer = answer + replace.get(Character.toString(s.charAt(i)));
        }
        
        return answer;
    }
}

이부분 개선해봄

while(true){
                    if(i>=index){
                        if(!skips.containsKey(Character.toString(c2))){
                            break;
                        }
                    }

내가 반대로 생각했었었음 둘 중 하나라고 false이면 break하는건데

나는 둘중 하나라도 true면 돌아가는게 하고싶었음

while((i>=index)||(!skips.containsKey(Character.toString(c2)))){

내가 하고싶은 조건은 이거지 그럼

실행 시간이 10.0초를 초과하여 실행이 중단되었습니다. 실행 시간이 더 짧은 다른 방법을 찾아보세요.

while문에서 나와지지 않음 ㅋㅋㅋㅋㅋ

그냥 if문으로 break하고 살련다

 

💡개선점

근데 String 에 contains가 달려있어서 굳이 쓸모도 없이 HashMap에다가 skip들 등록할 필요가 없음

그냥 skip.contains(?) 해주면 됨

 

다른 사람의 풀이

class Solution {
    public String solution(String s, String skip, int index) {
        StringBuilder answer = new StringBuilder();

        for (char letter : s.toCharArray()) {
            char temp = letter;
            int idx = 0;
            while (idx < index) {
                temp = temp == 'z' ? 'a' : (char) (temp + 1);
                if (!skip.contains(String.valueOf(temp))) {
                    idx += 1;
                }
            }
            answer.append(temp);
        }

        return answer.toString();
    }
}

나랑 기본 로직은 비슷한데 이거저거 안 활용하고 걍 한번에 간단하게 풀어버림 ㅜ....