first thought
- the same as word ladder problem, just need to record paths to the current word
solution
|
|
problem
wrong answer
Your input
“hit”
“cog”
[“hot”,”dot”,”dog”,”lot”,”log”,”cog”]
Your answer
[[“hit”,”hot”,”dot”,”dog”,”cog”]]
Expected answer
[[“hit”,”hot”,”lot”,”log”,”cog”],[“hit”,”hot”,”dot”,”dog”,”cog”]]
reason
it seems missing answers, maybe during the update part.
modification
- when the path size is equal, should not to clear all the list, if not so, it would clear the previous valid paths.
- change the outer list to set, to make sure there’s no duplicate in iterating.
|
|
problem
Surely TLE…
reason
full of sets…
modification
have seen the answer, the main idea of this problem is maintain a map of each word’s prewordlist, and using dfs to recursive the paths to the end.
|
|
problem
wrong answer, missing answers
reason
the timing of delete the wordSet is not proper.
modification
|
|
problem
duplicated answers…
Input:
“red”
“tax”
[“ted”,”tex”,”red”,”tax”,”tad”,”den”,”rex”,”pee”]
Output:
[[“red”,”ted”,”tad”,”tax”],[“red”,”ted”,”tex”,”tax”],[“red”,”rex”,”tex”,”tax”],[“red”,”ted”,”tex”,”tax”],[“red”,”rex”,”tex”,”tax”]]
Expected:
[[“red”,”ted”,”tad”,”tax”],[“red”,”ted”,”tex”,”tax”],[“red”,”rex”,”tex”,”tax”]]
reason
- still the timing.
- not the timing, it because queue2 may contain duplicated strings. some words in one layer may produce the same word, so the word be added to queue2 for several times.
modification
change queue2 to a set.
|
|