-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathWordLadder.java
56 lines (43 loc) · 1.33 KB
/
WordLadder.java
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
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
public class WordLadder {
public static void main(String[] args) {
// TODO Auto-generated method stub
String input = "hit";
String goal = "cog";
String[] matched = {"hot","dog","dot","lot","log","sog","mot","pot"};
LinkedList<String> availableList = new LinkedList<String>(Arrays.asList(matched));
LinkedList<Integer> distanceQueue = new LinkedList<Integer>();
LinkedList<String> queue = new LinkedList<String>();
availableList.add(goal);
queue.add(input);
distanceQueue.add(1);
int result = Integer.MAX_VALUE;
while(!queue.isEmpty()){
String currWord = queue.pop();
int currDist = distanceQueue.pop();
if(currWord.equals(goal)){
result = Math.min(result, currDist);
}
for(int i=0;i<currWord.length();i++){
char[] charArray = currWord.toCharArray();
for(char c ='a';c<='z';c++){
charArray[i] = c;
String newWord = new String(charArray);
if(availableList.contains(newWord)){
queue.add(newWord);
distanceQueue.add(currDist+1);
availableList.remove(newWord);
}
}
}
}
if (result < Integer.MAX_VALUE)
System.out.println("Steps are "+result);
else
System.err.println("Not Possible");
}
}