Last updated on October 5th, 2024 at 09:24 pm
Here, We see Word Ladder LeetCode Solution. This Leetcode problem is done in many programming languages like C++, Java, JavaScript, Python, etc. with different approaches.
List of all LeetCode Solution
Topics
Breadth-First Search
Companies
Amazon, Facebook, LinkedIn, Snapchat, Yelp
Level of Question
Hard
Word Ladder LeetCode Solution
Table of Contents
Problem Statement
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.
1. Word Ladder LeetCode Solution C++
class Solution { public: int ladderLength(string beginWord, string endWord, vector<string>& wordList) { unordered_set<string> dict(wordList.begin(), wordList.end()); queue<string> todo; todo.push(beginWord); int ladder = 1; while (!todo.empty()) { int n = todo.size(); for (int i = 0; i < n; i++) { string word = todo.front(); todo.pop(); if (word == endWord) { return ladder; } dict.erase(word); for (int j = 0; j < word.size(); j++) { char c = word[j]; for (int k = 0; k < 26; k++) { word[j] = 'a' + k; if (dict.find(word) != dict.end()) { todo.push(word); } } word[j] = c; } } ladder++; } return 0; } };
2. Word Ladder LeetCode Solution Java
class Solution { public int ladderLength(String beginWord, String endWord, List<String> wordList) { if (!wordList.contains(endWord)) { return 0; } Set<String> dict = new HashSet<>(wordList); Set<String> beginSet = new HashSet<>(); Set<String> endSet = new HashSet<>(); beginSet.add(beginWord); endSet.add(endWord); int step = 1; Set<String> visited = new HashSet<>(); while (!beginSet.isEmpty() && !endSet.isEmpty()) { if (beginSet.size() > endSet.size()) { Set<String> set = beginSet; beginSet = endSet; endSet = set; } Set<String> temp = new HashSet<>(); for (String word : beginSet) { char[] chs = word.toCharArray(); for (int i = 0; i < chs.length; i++) { for (char c = 'a'; c <= 'z'; c++) { char old = chs[i]; chs[i] = c; String target = String.valueOf(chs); if (endSet.contains(target)) { return step + 1; } if (!visited.contains(target) && dict.contains(target)) { temp.add(target); visited.add(target); } chs[i] = old; } } } beginSet = temp; step++; } return 0; } }
3. Word Ladder Solution JavaScript
var ladderLength = function(beginWord, endWord, wordList) { const wordSet = new Set(wordList) let queue = [beginWord]; let steps = 1; while(queue.length) { const next = []; for(let word of queue) { if(word === endWord) return steps; for(let i = 0; i < word.length; i++) { for(let j = 0; j < 26; j++) { const newWord = word.slice(0, i) + String.fromCharCode(j + 97) + word.slice(i+1); if(wordSet.has(newWord)) { next.push(newWord); wordSet.delete(newWord); } } } } queue = next steps++; } return 0; };
4. Word Ladder Solution Python
class Solution(object): def ladderLength(self, beginWord, endWord, wordDict): if endWord not in wordDict: return 0 length = 2 front, back = set([beginWord]), set([endWord]) wordDict = set(wordDict) wordDict.discard(beginWord) while front: front = wordDict & (set(word[:index] + ch + word[index+1:] for word in front for index in range(len(beginWord)) for ch in string.ascii_lowercase)) if front & back: return length length += 1 if len(front) > len(back): front, back = back, front wordDict -= front return 0