# wordList = ["hot","dot","dog","lot","log","cog"]
We are trying to turn a word, changing only 1 charater at a time to become the end word. And return the shortest path to that transformation. So we can visualize using a tree structure graph:
All words during the transformation, including the end word are children of the initial word. We can also conclude:
- we must use breath-first-search approach becuase we want to find as much path to the end node(word) as possible, to compare which one is the shortest.
- the level of the node reflects the number of changes (i.e length of the transformation)
- words appeared in the previous level shouldn’t appear in the following levels, or the nodes should be unique words.
- to reduce the time complexity we can find one path (must with same path length) to a same word instead of all. For an example, in the tree above both bit and him can turn into bim, we can only find bit=>bim and no need to find him=>bim since they offer the same path length, and path length is all we care about.
This would make more sense once you follow the logic of the codes.