The question:
Given an array of letters and integer n, return a list of all the words that can be formed (of length = n) using the letters. Discard words that contain the banned words. Banned words can have any length up to n.
Sample input:
letters = ['a', 'b', 'c']
bannedWords = ['aa', 'bc']
n = 3
The expected output is a list of all the words that do not contain the bannedWord substrings
e.g. 'aaa', 'aab' and 'aac' would not be in the output list because they contain 'aa', but 'aba' is fine.
My solution was:
Form all permutations using DFS -> O(letters.length ^ n)
For each formedWord, iterate over bannedWords and check if formedWord.contains(bannedWord)
Overall time complexity would be O((letters.length ^ n) * (bannedWords.length * n)), but apparently that could be improved to O((letters.length ^ n) * (n)). But I have no idea how, any ideas?
Thanks!