first thought
- create a set to maintain all the words
- split each word from index i (i from 0 to end), if the first part of it is in the set, and split the second recursively.
- remember each word should be split at least once.
solution
|
|
problem
has duplicated output
reason
a valid word may be added to the results for more than once because the different split during recursion.(e.g input: [a,b,ab,aa,aab] so ‘aab’ is split to ‘a ab’ and ‘aa b’ are both ok)
modification
change the results from List to Set
|
|
problem
TLE
reason
the same as the previous problem, check one word for several times
modification
if a word is valid, then jump out of the recursion and continue to check the next one.And this can also fix the first duplicate problem.
|
|