๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

์ฝ”๋”ฉํ…Œ์ŠคํŠธ/LeetCode

[LeetCode] Unique Morse Code Words

๋ฌธ์ œ๋งํฌ

 

Unique Morse Code Words - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

 

์ฃผ์–ด์ง„ ๋ชจ์Šค ๊ธฐํ˜ธ ๋ฐฐ์—ด์€ ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž์˜ ๋ฒˆํ˜ธ์™€ ์ผ์น˜ํ•œ๋‹ค. ( a === ".-", b === "-..." ... )
[".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]โ€‹

๋‹จ์–ด๊ฐ€ ์ฃผ์–ด์งˆ๋•Œ, ํ•ด๋‹น ๋‹จ์–ด์˜ ๋ชจ์Šค๊ธฐํ˜ธ๋ฅผ ๊ตฌํ•ด ๋ฐฐ์—ด์— ์ €์žฅํ•˜๊ณ  ์ค‘๋ณต๋˜์ง€ ์•Š๋Š” ๋ชจ์Šค๊ธฐํ˜ธ์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค.

์ ‘๊ทผ ๋ฐฉ๋ฒ•
์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž์™€ ๋ชจ์Šค ๊ธฐํ˜ธ ๋ฒˆํ˜ธ๊ฐ€ ๊ฐ™์œผ๋ฏ€๋กœ charCodeAt()์„ ํ†ตํ•ด ์•„์Šคํ‚ค ์ฝ”๋“œ ๋ฒˆํ˜ธ๋กœ ์ ‘๊ทผํ•˜๋Š” ๋ฐฉ์‹์„ ์ฑ„ํƒํ•˜์˜€๋‹ค. 

const morseCodeArr= []
    
words.forEach(word => {
    let morseCode = "";
    for(let char of word){
        morseCode += morseSign[char.charCodeAt() - 97]
    }
    morseCodeArr.push(morseCode)
})โ€‹

๋นˆ ๋ฐฐ์—ด์„ ํ•˜๋‚˜ ์ƒ์„ฑํ•˜๊ณ  ๋ชจ๋“  ๋‹จ์–ด๋ฅผ ์ˆœํšŒํ•˜๋ฉฐ ๊ฐ ๋‹จ์–ด์˜ ๋ฌธ์ž์˜ ์•„์Šคํ‚ค ์ฝ”๋“œ๋ฅผ ๊ตฌํ•ด - 97์„ ํ•œ๋‹ค.
( 'a'์˜ ์•„์Šคํ‚ค ์ฝ”๋“œ๋Š” 97์ด๋ฏ€๋กœ ๋ชจ๋“  ์˜์–ด ์†Œ๋ฌธ์ž์— -97์„ ํ•˜๋ฉด 0~26๊นŒ์ง€์˜ ๊ฐ’์„ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค.)

๊ทธ๋ ‡๊ฒŒ ๋นˆ๋ฐฐ์—ด์— ๋ชจ๋“  ๋ชจ์Šค ๊ธฐํ˜ธ๋ฅผ ์ €์žฅํ•˜๊ณ  ์ค‘๋ณต์„ ์ œ๊ฑฐํ•˜๋ฉด ์ •๋‹ต์„ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค.

 

/**
 * @param {string[]} words
 * @return {number}
 */
var uniqueMorseRepresentations = function(words) {
    const morseSign = [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."];
    
    const morseCodeArr= []
    
    words.forEach(word => {
        let morseCode = "";
        for(let char of word){
            morseCode += morseSign[char.charCodeAt() - 97]
        }
        morseCodeArr.push(morseCode)
    })
    
    return (new Set(morseCodeArr)).size
};

'์ฝ”๋”ฉํ…Œ์ŠคํŠธ > LeetCode' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[LeetCode] Rotate Image  (0) 2022.08.30
[LeetCode] Reduce Array Size to The Half  (0) 2022.08.18
[LeetCode] Combination Sum IV  (0) 2022.08.05
[LeetCode] My Calendar I  (0) 2022.08.03
[LeetCode] Kth Smallest Element In A Sorted Matrix  (0) 2022.08.02