For convenience, I have put the test cases within the same file as the functions.
To run the code:
-
Compile the TypeScript code with the
--target
flag set toes6
:
tsc --target es6 index.ts
-
Run the compiled code:
node index.js
The expected output:
[ 'ate', 'eat', 'tea' ]
[ 'dog', 'do', 'god', 'goo', 'go', 'good' ]
[ 'oo', 'ooo' ]
[]
(Optional) To run the js solution, simply run:
node js_solution.js
My solution consists 3 functions:
findWords
: the main function used to iterate over an input array and filter it using thecanBeFormed
function.canBeFormed
: the helper function used byfindWords
to determine if a single word can be formed by rearranging the characters of another word.wordToMap
: the helper function to create a map of character frequencies from an input string. For example, "food" to { f:1, o:2, d:1 }