Created
March 29, 2017 21:05
-
-
Save mrmlnc/c6b6b4ed2073dfe80e89b58bb0fe2217 to your computer and use it in GitHub Desktop.
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
const words = ["eat", "tea", "tan", "ate", "nat", "bat"]; | |
function updateDictionary(dict: object, word: string): void { | |
const key = word.split('').sort().join(''); | |
if (!dict.hasOwnProperty(key)) { | |
dict[key] = []; | |
} | |
dict[key].push(word); | |
} | |
function router(words: string[]): void { | |
const dictionary = {}; | |
for (let i = 0; i < words.length; i++) { | |
updateDictionary(dictionary, words[i]); | |
} | |
const result = Object.keys(dictionary); | |
for (let i = 0; i < result.length; i++) { | |
const key: string[] = result[i]; | |
result[i] = dictionary[key]; | |
} | |
console.log(result); | |
} | |
router(words); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment