Skip to content

Instantly share code, notes, and snippets.

@mrmlnc
Created March 29, 2017 21:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrmlnc/c6b6b4ed2073dfe80e89b58bb0fe2217 to your computer and use it in GitHub Desktop.
Save mrmlnc/c6b6b4ed2073dfe80e89b58bb0fe2217 to your computer and use it in GitHub Desktop.
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