A transformation sequence from word beginWord to word endWord using a dictionary wordList is a sequence of words beginWord -> s1 -> s2 -> ... -> sk such that:
1 <= i <= k is in wordList. Note that beginWord does not need to be in wordList.Given two words, beginWord and endWord, and a dictionary wordList, return the number of words in the shortest transformation sequence from beginWord to endWord, or 0 if no such sequence exists.
Input: beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log","cog"]
Output: 5
Explanation: One shortest transformation sequence is "hit" -> "hot" -> "dot" -> "dog" -> cog", which is 5 words long.
Input: beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log"]
Output: 0
Explanation: The endWord "cog" is not in wordList, therefore there is no valid transformation sequence.
1 <= beginWord.length <= 10endWord.length == beginWord.length1 <= wordList.length <= 5000wordList[i].length == beginWord.lengthbeginWord, endWord, and wordList[i] consist of lowercase English letters.beginWord != endWordwordList are unique.The Word Ladder problem can be thought of as a graph traversal problem where each word in the word list is a node and there is an edge between two nodes if they differ by exactly one character. The problem is then to find the shortest path from the beginWord to the endWord.
Breadth-First Search (BFS) is a perfect fit for this type of problem because it explores all nodes at the present "depth" level before moving on to nodes at the next depth level, thereby ensuring that we find the shortest path.
Bidirectional BFS is an optimization of the traditional BFS technique. Instead of searching from one end to the other, we simultaneously search from both the beginWord and the endWord towards each other. The search halts when the two searches meet. This significantly reduces the search space and can greatly increase efficiency, especially as the length of transformation increases.