-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWordLadder.java
More file actions
247 lines (189 loc) · 8.38 KB
/
WordLadder.java
File metadata and controls
247 lines (189 loc) · 8.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package Algorithms.Graphs;
import java.util.*;
/**
* @author Srinivas Vadige, srinivas.vadige@gmail.com
* @since 03 Jan 2026
* @link 127. Word Ladder <a href="https://leetcode.com/problems/word-ladder/">LeetCode Link</a>
* @topics Graph, Hash Table, DFS, BFS
* @companies CleverTap(8), Google(5), Meta(4), Amazon(4), Microsoft(3), Bloomberg(3), Oracle(3), ByteDance(2), Citadel(2), Okta(2), LinkedIn(5), Apple(3), TikTok(2), ServiceNow(2), Expedia(2), Reddit(18), Snap(8), Uber(6), eBay(6), Adobe(4), Flipkart(3), Visa(3), Salesforce(3), Box(3), The Trade Desk(3)
* @see Algorithms.Graphs.MinimumGeneticMutation
*/
public class WordLadder {
public static void main(String[] args) {
String beginWord = "hit", endWord = "cog";
List<String> wordList = new ArrayList<>(Arrays.asList("hot","dot","dog","lot","log","cog"));
System.out.println("ladderLength Using BFS 1 -> " + ladderLengthUsingBfs1(beginWord, endWord, wordList));
System.out.println("ladderLength Using BFS 2 -> " + ladderLengthUsingBfs2(beginWord, endWord, wordList));
System.out.println("ladderLength Using BFS 3 -> " + ladderLengthUsingBfs3(beginWord, endWord, wordList));
System.out.println("ladderLength Using BFS 4 -> " + ladderLengthUsingBfs4(beginWord, endWord, wordList));
}
/**
* @TimeComplexity O(M^2 * N), where M is the length of each word and N wordList size
* @SpaceComplexity O(N)
* same as {@link Algorithms.Graphs.MinimumGeneticMutation#minMutationUsingBfs4}
*/
public static int ladderLengthUsingBfs1(String beginWord, String endWord, List<String> wordList) {
int k = beginWord.length();
Set<String> wordSet = new HashSet<>(wordList);
if (wordSet.add(endWord)) return 0;
Queue<String> q = new LinkedList<>();
Set<String> seen = new HashSet<>();
seen.add(beginWord);
q.add(beginWord);
int transformations = 1;
while(!q.isEmpty()) {
int size = q.size();
while (size-- > 0) {
String currWord = q.poll();
if (currWord.equals(endWord)) return transformations;
char[] currWordArr = currWord.toCharArray();
for (int i=0; i<k; i++) {
char oldChar = currWordArr[i];
for(char c='a'; c<='z'; c++) {
if (c==oldChar) continue;
currWordArr[i] = c;
String nextWord = new String(currWordArr);
if (!seen.contains(nextWord) && wordSet.contains(nextWord)) {
seen.add(nextWord);
q.add(nextWord);
}
}
currWordArr[i] = oldChar;
}
}
transformations++;
}
return 0;
}
/**
* @TimeComplexity O(M^2 * N), where M is the length of each word and N wordList size
* @SpaceComplexity O(N)
*/
public static int ladderLengthUsingBfs2(String beginWord, String endWord, List<String> wordList) {
Set<String> wordSet = new HashSet<>(wordList);
if (!wordSet.contains(endWord)) return 0;
Queue<String> queue = new LinkedList<>();
queue.offer(beginWord);
int steps = 1;
while (!queue.isEmpty()) {
int size = queue.size();
while (size-- > 0) {
String word = queue.poll();
if (word.equals(endWord)) return steps;
for (int i = 0; i < word.length(); i++) {
for (char c = 'a'; c <= 'z'; c++) {
char[] chars = word.toCharArray();
chars[i] = c;
String pattern = new String(chars);
if (wordSet.contains(pattern)) {
queue.offer(pattern);
wordSet.remove(pattern);
}
}
}
}
steps++;
}
return 0;
}
/**
* @TimeComplexity O(M^2 * N), where M is the length of each word and N wordList size
* @SpaceComplexity O(MN)
*/
public static int ladderLengthUsingBfs3(String beginWord, String endWord, List<String> wordList) {
if (!wordList.contains(endWord)) return 0;
// pattern -> list of words
Map<String, List<String>> neighbors = new HashMap<>();
wordList.add(beginWord);
for (String word : wordList) {
for (int i = 0; i < word.length(); i++) {
String pattern = word.substring(0, i) + "*" + word.substring(i + 1);
neighbors.computeIfAbsent(pattern, k -> new ArrayList<>()).add(word);
}
}
Set<String> visit = new HashSet<>();
Queue<String> q = new ArrayDeque<>();
visit.add(beginWord);
q.offer(beginWord);
int transformations = 1;
while (!q.isEmpty()) {
int size = q.size();
while (size-- > 0) {
String word = q.poll();
if (word.equals(endWord)) return transformations;
for (int i = 0; i < word.length(); i++) {
String pattern = word.substring(0, i) + "*" + word.substring(i + 1);
for (String neiWord : neighbors.getOrDefault(pattern, Collections.emptyList())) {
if (!visit.contains(neiWord)) {
visit.add(neiWord);
q.offer(neiWord);
}
}
}
}
transformations++;
}
return 0;
}
/**
* @TimeComplexity O(M^2 * N), where M is the length of each word and N wordList size
* @SpaceComplexity O(MN)
* but much faster than {@link #ladderLengthUsingBfs3}
*/
static class Pair extends AbstractMap.SimpleEntry<String, Integer>{Pair(String k, Integer v){super(k,v);}}
private static int L = 0;
private static Map<String, List<String>> allComboDict = new HashMap<>();
public static int ladderLengthUsingBfs4(String beginWord, String endWord, List<String> wordList) {
if (!wordList.contains(endWord)) return 0;
L = beginWord.length();
allComboDict = new HashMap<>();
wordList.forEach(word -> {
for (int i = 0; i < L; i++) {
String pattern = word.substring(0, i) + '*' + word.substring(i + 1, L);
List<String> transformations = allComboDict.getOrDefault(pattern, new ArrayList<>());
transformations.add(word);
allComboDict.put(pattern, transformations);
}
});
Queue<Pair> Q_begin = new LinkedList<>();
Queue<Pair> Q_end = new LinkedList<>();
Q_begin.add(new Pair(beginWord, 1));
Q_end.add(new Pair(endWord, 1));
Map<String, Integer> visitedBegin = new HashMap<>();
Map<String, Integer> visitedEnd = new HashMap<>();
visitedBegin.put(beginWord, 1);
visitedEnd.put(endWord, 1);
int ans = -1;
while (!Q_begin.isEmpty() && !Q_end.isEmpty()) {
if (Q_begin.size() <= Q_end.size()) {
ans = visitWordNode(Q_begin, visitedBegin, visitedEnd);
} else {
ans = visitWordNode(Q_end, visitedEnd, visitedBegin);
}
if (ans > -1) {
return ans;
}
}
return 0;
}
private static int visitWordNode(Queue<Pair> Q, Map<String, Integer> visited, Map<String, Integer> othersVisited) {
for (int j = Q.size(); j > 0; j--) {
Pair node = Q.remove();
String word = node.getKey();
int level = node.getValue();
for (int i = 0; i < L; i++) {
String pattern = word.substring(0, i) + '*' + word.substring(i + 1, L);
for (String adjacentWord : allComboDict.getOrDefault(pattern,new ArrayList<>())) {
if (othersVisited.containsKey(adjacentWord)) {
return level + othersVisited.get(adjacentWord);
}
if (!visited.containsKey(adjacentWord)) {
visited.put(adjacentWord, level + 1);
Q.add(new Pair(adjacentWord, level + 1));
}
}
}
}
return -1;
}
}