Sale
Summer Offer: $190/yr $99/yr
00 : 00 : 00 : 00
Get this deal
Coding Round 2 Senior programming

Word Ladder

Given two words, beginWord and endWord, and a dictionary's word list, return the number of words that need to be changed to convert beginWord to endWord. Each transformed word must exist in the word list. Note that you can only change one letter at a time, and the new word must be different from the original.
Function Signature: def ladderLength(beginWord: str, endWord: str, wordList: List[str]) -> int:
Example 1:
Input: beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log","cog"]
Output: 5
Explanation: One possible transformation is "hit" -> "hot" -> "dot" -> "dog" -> "cog", which has a length of 5.
Example 2:
Input: beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log"]
Output: 0
Explanation: The endWord is not in the word list, so it cannot be reached.
Constraints:
  • 1 <= beginWord.length <= 10

  • 1 <= endWord.length <= 10

  • 1 <= wordList.length <= 1000

  • beginWord != endWord.
Suggested Answer

Trusted by 100+ professionals preparing for interviews

Trusted by 100+ professionals 50+ Company Question Banks 5+ Supported Languages

Practice More Questions Like This

Generate unlimited interview questions with structured answers, code runner, and AI-powered walkthroughs.

Get Started Free

More Coding Round 2 Interview Prep

LeetCode #159 - Longest Substring with At Most Two Distinct Characters
Coding Round 1 · Mid-Level
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Link copied to clipboard