Contents

๋ชจ์Šค๋ถ€ํ˜ธ 1 (with.Java)

   Feb 5, 2024     3 min read

โ€œ๋ชจ์Šค๋ถ€ํ˜ธ 1โ€ ๋ฌธ์ œ์— ๋Œ€ํ•˜์—ฌ ์•Œ์•„๋ณธ ๊ธ€์ž…๋‹ˆ๋‹ค.

์ฝ”๋”ฉ ํ…Œ์ŠคํŠธ ๋ฌธ์ œ๋ฅผ ํ’€๋ฉฐ, ํ’€์—ˆ๋˜ ๋ฌธ์ œ์— ๋Œ€ํ•œ ํšŒ๊ณ ์™€ ๋‹ค๋ฅธ ํ’€์ด ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด๋ฉฐ, ์•Œ์•„๊ฐ€๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค.

๋ฌธ์ œ์— ๋Œ€ํ•ด ๋จผ์ € ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

๋ฌธ์ œ

๋จธ์“ฑ์ด๋Š” ์นœ๊ตฌ์—๊ฒŒ ๋ชจ์Šค๋ถ€ํ˜ธ๋ฅผ ์ด์šฉํ•œ ํŽธ์ง€๋ฅผ ๋ฐ›์•˜์Šต๋‹ˆ๋‹ค.

๊ทธ๋ƒฅ์€ ์ฝ์„ ์ˆ˜ ์—†์–ด ์ด๋ฅผ ํ•ด๋…ํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ๋งŒ๋“ค๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

๋ฌธ์ž์—ด letter๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ, letter๋ฅผ ์˜์–ด ์†Œ๋ฌธ์ž๋กœ ๋ฐ”๊พผ ๋ฌธ์ž์—ด์„ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด๋ณด์„ธ์š”.

๋ชจ์Šค๋ถ€ํ˜ธ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

morse = {
'.-':'a','-...':'b','-.-.':'c','-..':'d','.':'e','..-.':'f',
'--.':'g','....':'h','..':'i','.---':'j','-.-':'k','.-..':'l',
'--':'m','-.':'n','---':'o','.--.':'p','--.-':'q','.-.':'r',
'...':'s','-':'t','..-':'u','...-':'v','.--':'w','-..-':'x',
'-.--':'y','--..':'z'
}

์ œํ•œ์‚ฌํ•ญ

  • 1 โ‰ค letter์˜ ๊ธธ์ด โ‰ค 1,000
  • return๊ฐ’์€ ์†Œ๋ฌธ์ž์ž…๋‹ˆ๋‹ค.
  • letter์˜ ๋ชจ์Šค๋ถ€ํ˜ธ๋Š” ๊ณต๋ฐฑ์œผ๋กœ ๋‚˜๋ˆ„์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค.
  • letter์— ๊ณต๋ฐฑ์€ ์—ฐ์†์œผ๋กœ ๋‘ ๊ฐœ ์ด์ƒ ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
  • ํ•ด๋…ํ•  ์ˆ˜ ์—†๋Š” ํŽธ์ง€๋Š” ์ฃผ์–ด์ง€์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
  • ํŽธ์ง€์˜ ์‹œ์ž‘๊ณผ ๋์—๋Š” ๊ณต๋ฐฑ์ด ์—†์Šต๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ์‹œ

letterresult
โ€œโ€ฆ. . .-.. .-.. โ€”โ€โ€œhelloโ€
โ€.โ€“. -.โ€“ - โ€ฆ. โ€” -.โ€โ€œpythonโ€

๋ฌธ์ œ์— ๋Œ€ํ•œ ๋‚˜์˜ ํ’€์ด

class Solution {
    public String solution(String letter) {
        String[] morseArray = letter.split(" ");
        StringBuilder answer = new StringBuilder();
        String[] morseAlphabet = {
            ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---",
            "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-",
            "..-", "...-", ".--", "-..-", "-.--", "--.."
        };

        for (String morse : morseArray){
            for (int i = 0; i < morseAlphabet.length; i++) {
                if (morse.equals(morseAlphabet[i])) {
                    char decodedChar = (char) ('a' + i);
                    answer.append(decodedChar);
                }
            }
        }

        return answer.toString();
    }
}

ํ’€์ด ์„ค๋ช…

split: ๋ฌธ์ž์—ด์„ ๊ณต๋ฐฑ์„ ๊ธฐ์ค€์œผ๋กœ ๋‚˜๋ˆ„์–ด ๋ฐฐ์—ด๋กœ ๋งŒ๋“ญ๋‹ˆ๋‹ค.

์—ฌ๊ธฐ์„œ๋Š” Morse ์ฝ”๋“œ๋ฅผ ๊ณต๋ฐฑ์„ ๊ธฐ์ค€์œผ๋กœ ๋‚˜๋ˆ„์–ด ๋ฐฐ์—ด๋กœ ์ €์žฅํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

(char) (โ€˜aโ€™ + i): ASCII ์ฝ”๋“œ๋ฅผ ํ™œ์šฉํ•˜์—ฌ i ๊ฐ’์„ ์•ŒํŒŒ๋ฒณ์œผ๋กœ ๋ณ€ํ™˜ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. โ€˜aโ€™ + i๋Š” i๊ฐ€ 0์ผ ๋•Œ โ€˜aโ€™๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ , i๊ฐ€ 1์ผ ๋•Œ โ€˜bโ€™๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ์‹์œผ๋กœ ๋™์ž‘ํ•ฉ๋‹ˆ๋‹ค.

์™œ (char) (โ€˜aโ€™ + i)๋ฅผ ์‚ฌ์šฉํ–ˆ๋Š”๊ฐ€ i ๊ฐ’์— ๋”ฐ๋ผ โ€˜aโ€™์—์„œ๋ถ€ํ„ฐ ์ฐจ๋ก€๋กœ ์•ŒํŒŒ๋ฒณ์„ ์–ป๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

(char)๋Š” ์ •์ˆ˜๋ฅผ ๋ฌธ์ž๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ์บ์ŠคํŒ…์„ ์˜๋ฏธํ•˜๋ฉฐ, โ€˜aโ€™ + i๋Š” ASCII ์ฝ”๋“œ์—์„œ i์— ํ•ด๋‹นํ•˜๋Š” ์•ŒํŒŒ๋ฒณ์„ ์–ป๊ธฐ ์œ„ํ•œ ํ‘œํ˜„์‹์ž…๋‹ˆ๋‹ค.

์ด๋ฅผ ํ†ตํ•ด Morse ์ฝ”๋“œ๋ฅผ ์•ŒํŒŒ๋ฒณ์œผ๋กœ ๋ณ€ํ™˜ํ•˜๊ณ  ๊ทธ ๊ฒฐ๊ณผ๋ฅผ StringBuilder๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ตœ์ข… ๋ฌธ์ž์—ด๋กœ ๋งŒ๋“ค๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

Java์—์„œ๋Š” ๋ฐฐ์—ด์˜ ์ธ๋ฑ์Šค๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ๊ฐ์˜ ๊ฐ’์„ ์ง์ ‘ ๋งคํ•‘ํ•˜๋Š” ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๊ทธ๋Ÿฌ๋‚˜ ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ์ด์œ ๋กœ ๋งคํ•‘์ด ํ•„์š”ํ•œ ๊ฒฝ์šฐ, HashMap ๋˜๋Š” ๋‹ค๋ฅธ ๋งคํ•‘ ๊ตฌ์กฐ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ํŽธ๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

HashMap์„ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ

  • ๋™์ ์ธ ๋งคํ•‘: ์ƒˆ๋กœ์šด ์•ŒํŒŒ๋ฒณ์ด๋‚˜ Morse ์ฝ”๋“œ๊ฐ€ ์ถ”๊ฐ€๋˜๊ฑฐ๋‚˜ ๋ณ€๊ฒฝ๋  ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ, HashMap์„ ์‚ฌ์šฉํ•˜๋ฉด ๋™์ ์œผ๋กœ ๋งคํ•‘์„ ์กฐ์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ํ‚ค-๊ฐ’ ์Œ ๊ด€๋ฆฌ: HashMap์„ ์‚ฌ์šฉํ•˜๋ฉด ํ‚ค์™€ ๊ฐ’์˜ ์Œ์„ ์‰ฝ๊ฒŒ ์ถ”๊ฐ€, ์‚ญ์ œ, ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์˜ˆ์‹œ:

// ์ดˆ๊ธฐํ™” ๋ธ”๋ก์„ ์‚ฌ์šฉํ•˜์—ฌ HashMap ์ดˆ๊ธฐํ™”
    static {
        morseMap = new HashMap<>();
        morseMap.put(".-", 'a');
        morseMap.put("-...", 'b');
        morseMap.put("-.-.", 'c');
        ...
     }

๋ฐฐ์—ด์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ

  • ์ •์ ์ธ ๋งคํ•‘: ๋งคํ•‘์ด ๋ณ€๊ฒฝ๋˜์ง€ ์•Š๊ณ  ๊ณ ์ •๋œ ๊ฒฝ์šฐ ๋ฐฐ์—ด์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ๊ฐ„๋‹จํ•œ ๊ตฌ์กฐ: ๋งคํ•‘์ด ๊ฐ„๋‹จํ•˜๊ณ  ๋ณ€๊ฒฝ๋  ๊ฐ€๋Šฅ์„ฑ์ด ๋‚ฎ์€ ๊ฒฝ์šฐ ๋ฐฐ์—ด์„ ์‚ฌ์šฉํ•˜์—ฌ ์ฝ”๋“œ๋ฅผ ๊ฐ„๊ฒฐํ•˜๊ฒŒ ์œ ์ง€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋”ฐ๋ผ์„œ ์ƒํ™ฉ์— ๋”ฐ๋ผ ์ ์ ˆํ•œ ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ๋ฅผ ์„ ํƒํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค. ๊ฐ๊ฐ์˜ ์žฅ๋‹จ์ ์„ ๊ณ ๋ คํ•˜์—ฌ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.