Word Ladder - aloalgo

Word Ladder

Medium

You are given two words, beginWord and endWord, and a dictionary of words wordList.

Your task is to find the length of the shortest transformation sequence from beginWord to endWord.

You may assume that:

  • Each transformation step involves changing exactly one letter of the current word.
  • All intermediate words in the sequence (excluding beginWord) must be present in wordList.
  • beginWord does not need to be in wordList.

Return the length of the shortest transformation sequence or 0 if no such transformation sequence exists.

Example 1

Inputs
beginWord = "bat"
endWord = "cat"
wordList = ["bat", "rat", "cat"]
Output
2
Explanation:

"bat" can be transformed to "cat" by changing 'b' to 'c'.

Example 2

Inputs
beginWord = "cold"
endWord = "warm"
wordList = ["cold", "cord", "card", "ward", "warm"]
Output
5
Explanation:

One possible transformation sequence is "cold" -> "cord" -> "card" -> "ward" -> "warm".

Example 3

Inputs
beginWord = "lamp"
endWord = "fork"
wordList = ["lamp", "lamb", "limb", "lime", "like", "bike", "bake", "bake", "bake", "fork"]
Output
0
Explanation:

There is no possible transformation sequence from "lamp" to "fork" using the provided word list.

Loading...
Inputs
beginWord = "bat"
endWord = "cat"
wordList = ["bat", "rat", "cat"]
Output
2

Hello! I am your ✨ AI assistant. I can provide you hints, explanations, give feedback on your code, and more. Just ask me anything related to the problem you're working on!