Leetcode
Title
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 804. Unique Morse Code Words (Easy) | |
// https://leetcode.com/problems/unique-morse-code-words/ | |
// 1. Naive answer: | |
const alphabetTable: {[prop:string]: string} = { | |
'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': "--.." | |
} | |
function uniqueMorseRepresentations(words: string[]): number { | |
let uniqueTransformations = new Set<string>() | |
words.forEach(word => { | |
let transformation: string = '' | |
for(let i = 0; i < word.length; i++) { | |
transformation += alphabetTable[word[i]] | |
} | |
console.log(transformation) | |
uniqueTransformations.add(transformation) | |
}) | |
return uniqueTransformations.size | |
}; | |
// 2. Refined Answer: | |
function uniqueMorseRepresentations_refined(words: string[]): number { | |
const morseCodes: string[] = [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."] | |
const transformationSet = new Set<string>() | |
words.forEach(word => { | |
let wordInMorse = '' | |
for (let i = 0; i < word.length; i++) { | |
wordInMorse += morseCodes[word[i].charCodeAt(0) - 'a'.charCodeAt(0)] | |
} | |
transformationSet.add(wordInMorse) | |
}) | |
return transformationSet.size | |
} | |
console.log(uniqueMorseRepresentations_refined(["gin","zen","gig","msg"])) |
test1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 804. Unique Morse Code Words (Easy) | |
// https://leetcode.com/problems/unique-morse-code-words/ | |
// 1. Naive answer: | |
const alphabetTable: {[prop:string]: string} = { | |
'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': "--.." | |
} | |
function uniqueMorseRepresentations(words: string[]): number { | |
let uniqueTransformations = new Set<string>() | |
words.forEach(word => { | |
let transformation: string = '' | |
for(let i = 0; i < word.length; i++) { | |
transformation += alphabetTable[word[i]] | |
} | |
console.log(transformation) | |
uniqueTransformations.add(transformation) | |
}) | |
return uniqueTransformations.size | |
}; | |
// 2. Refined Answer: | |
function uniqueMorseRepresentations_refined(words: string[]): number { | |
const morseCodes: string[] = [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."] | |
const transformationSet = new Set<string>() | |
words.forEach(word => { | |
let wordInMorse = '' | |
for (let i = 0; i < word.length; i++) { | |
wordInMorse += morseCodes[word[i].charCodeAt(0) - 'a'.charCodeAt(0)] | |
} | |
transformationSet.add(wordInMorse) | |
}) | |
return transformationSet.size | |
} | |
console.log(uniqueMorseRepresentations_refined(["gin","zen","gig","msg"])) |
test2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 804. Unique Morse Code Words (Easy) | |
// https://leetcode.com/problems/unique-morse-code-words/ | |
// 1. Naive answer: | |
const alphabetTable: {[prop:string]: string} = { | |
'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': "--.." | |
} | |
function uniqueMorseRepresentations(words: string[]): number { | |
let uniqueTransformations = new Set<string>() | |
words.forEach(word => { | |
let transformation: string = '' | |
for(let i = 0; i < word.length; i++) { | |
transformation += alphabetTable[word[i]] | |
} | |
console.log(transformation) | |
uniqueTransformations.add(transformation) | |
}) | |
return uniqueTransformations.size | |
}; | |
// 2. Refined Answer: | |
function uniqueMorseRepresentations_refined(words: string[]): number { | |
const morseCodes: string[] = [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."] | |
const transformationSet = new Set<string>() | |
words.forEach(word => { | |
let wordInMorse = '' | |
for (let i = 0; i < word.length; i++) { | |
wordInMorse += morseCodes[word[i].charCodeAt(0) - 'a'.charCodeAt(0)] | |
} | |
transformationSet.add(wordInMorse) | |
}) | |
return transformationSet.size | |
} | |
console.log(uniqueMorseRepresentations_refined(["gin","zen","gig","msg"])) |