Given two words, beginWord
and endWord
, and a dictionary wordList
, return the length of the shortest transformation sequence from beginWord
to endWord
, such that:
- Only one letter can be changed at a time.
- Each transformed word must exist in the wordList.
Return 0 if no such sequence.
- BFS for shortest path in implicit graphs
- Graph modeling of words
Input: beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log","cog"]
Output: 5
Explanation: "hit" -> "hot" -> "dot" -> "dog" -> "cog"
Input: beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log"]
Output: 0
def ladder_length(beginWord: str, endWord: str, wordList: list[str]) -> int:
# Implement your solution here
pass