Leetcode

Title

// 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"]))
view raw 804.ts hosted with ❤ by GitHub

test1

// 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"]))
view raw 804.ts hosted with ❤ by GitHub

test2

// 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"]))
view raw 804.ts hosted with ❤ by GitHub