In this post, we are going to solve the Word Ladder Leetcode Solution problem of Leetcode. This Leetcode problem is done in many programming languages like C++, Java, and Python.

Problem
A transformation sequence from word beginWord
to word endWord
using a dictionary wordList
is a sequence of words beginWord -> s1 -> s2 -> ... -> sk
such that:
- Every adjacent pair of words differs by a single letter.
- Every
si
for1 <= i <= k
is inwordList
. Note thatbeginWord
does not need to be inwordList
. sk == endWord
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.
Example 1:
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.
Example 2:
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.
Constraints:
1 <= beginWord.length <= 10
endWord.length == beginWord.length
1 <= wordList.length <= 5000
wordList[i].length == beginWord.length
beginWord
,endWord
, andwordList[i]
consist of lowercase English letters.beginWord != endWord
- All the words in
wordList
are unique.
Now, let’s see the leetcode solution of Word Ladder Leetcode Solution.
Word Ladder Leetcode Solution in Python
class Solution: def ladderLength(self, beginWord: str, endWord: str, wordList: List[str]) -> int: wordSet = set(wordList) if endWord not in wordSet: return 0 ans = 0 q = deque([beginWord]) while q: ans += 1 for _ in range(len(q)): wordList = list(q.popleft()) for i, cache in enumerate(wordList): for c in string.ascii_lowercase: wordList[i] = c word = ''.join(wordList) if word == endWord: return ans + 1 if word in wordSet: q.append(word) wordSet.remove(word) wordList[i] = cache return 0
Word Ladder Leetcode Solution in CPP
class Solution { public: int ladderLength(string beginWord, string endWord, vector<string>& wordList) { unordered_set<string> wordSet(begin(wordList), end(wordList)); if (!wordSet.count(endWord)) return 0; int ans = 0; queue<string> q{{beginWord}}; while (!q.empty()) { ++ans; for (int sz = q.size(); sz > 0; --sz) { string word = q.front(); q.pop(); for (int i = 0; i < word.length(); ++i) { const char cache = word[i]; for (char c = 'a'; c <= 'z'; ++c) { word[i] = c; if (word == endWord) return ans + 1; if (wordSet.count(word)) { q.push(word); wordSet.erase(word); } } word[i] = cache; } } } return 0; } };
Word Ladder Leetcode Solution in Java
class Solution { public int ladderLength(String beginWord, String endWord, List<String> wordList) { Set<String> wordSet = new HashSet<>(wordList); if (!wordSet.contains(endWord)) return 0; int ans = 0; Queue<String> q = new ArrayDeque<>(Arrays.asList(beginWord)); while (!q.isEmpty()) { ++ans; for (int sz = q.size(); sz > 0; --sz) { StringBuilder sb = new StringBuilder(q.poll()); for (int i = 0; i < sb.length(); ++i) { final char cache = sb.charAt(i); for (char c = 'a'; c <= 'z'; ++c) { sb.setCharAt(i, c); final String word = sb.toString(); if (word.equals(endWord)) return ans + 1; if (wordSet.contains(word)) { q.offer(word); wordSet.remove(word); } } sb.setCharAt(i, cache); } } } return 0; } }
Note: This problem Word Ladder is generated by Leetcode but the solution is provided by Chase2learn This tutorial is only for Educational and Learning purpose.