코린이의 소소한 공부노트

[LeetCode/Easy] 2325. Decode the Message 본문

코딩테스트 풀이/JAVA

[LeetCode/Easy] 2325. Decode the Message

무지맘 2023. 5. 2. 09:57

1. Input

1) String key

2) String message

 

2. Output

1) key를 이용해 message를 해독한 결과를 반환

- key에 나온 순서대로 a부터 매칭한다.

- 이때, key에 중복된 알파벳이 있다면 맨 처음 나온 것만 고려한다.

- 공백 문자를 공백 문자로 매칭한다.

 

3. Constraint

1) 26 <= key.length <= 2000

2) keymessage는 영어 소문자와 공백문자로 이루어져 있다.

3) key에는 각 영어 소문자가 최소 1개 이상 들어있다.

4) 1 <= message.length <= 2000

 

4. Example

Input: key = "the quick brown fox jumps over the lazy dog", message = "vkbs bs t suepuv" -> Output: "this is a secret"

설명: key - 알파벳의 관계는 다음 그림과 같다.

 

5. Code

1) 첫 코드(2023/05/02)

class Solution {
    public String decodeMessage(String key, String message) {
        ArrayList<Character> dict = new ArrayList<>();
        for(int i=0 ; i<key.length() ; i++){
            char c = key.charAt(i);
            if(c!=' ' && !dict.contains(c))
                dict.add(c);
        }
        char[] c = message.toCharArray();
        for(int i=0 ; i<c.length ; i++)
            if(c[i]!=' ')
                c[i] = (char)(dict.indexOf(c[i])+'a');
        return new String(c);
    }
}