-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWordLadder.java
More file actions
151 lines (131 loc) · 3.44 KB
/
WordLadder.java
File metadata and controls
151 lines (131 loc) · 3.44 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
/**
*
*/
package cc.dectinc.leetcode;
import java.util.*;
/**
* @author Dectinc
* @version Apr 8, 2015 8:13:39 PM
*
*/
public class WordLadder {
public int ladderLength(String start, String end, Set<String> dict) {
if (dict.size() == 0)
return 0;
dict.add(end);
LinkedList<String> wordQueue = new LinkedList<String>();
LinkedList<Integer> distanceQueue = new LinkedList<Integer>();
wordQueue.add(start);
distanceQueue.add(1);
// track the shortest path
int result = Integer.MAX_VALUE;
while (!wordQueue.isEmpty()) {
String currWord = wordQueue.pop();
Integer currDistance = distanceQueue.pop();
if (currWord.equals(end)) {
result = Math.min(result, currDistance);
}
for (int i = 0; i < currWord.length(); i++) {
char[] currCharArr = currWord.toCharArray();
for (char c = 'a'; c <= 'z'; c++) {
currCharArr[i] = c;
String newWord = new String(currCharArr);
if (dict.contains(newWord)) {
wordQueue.add(newWord);
distanceQueue.add(currDistance + 1);
dict.remove(newWord);
}
}
}
}
if (result < Integer.MAX_VALUE)
return result;
else
return 0;
}
public int ladderLength2(String start, String end, Set<String> dict) {
if (dict.size() == 0) {
return 0;
}
int wordLength = start.length();
dict.add(end);
LinkedList<String> words = new LinkedList<String>();
words.add(start);
LinkedList<Integer> distances = new LinkedList<Integer>();
distances.add(1);
dict.remove(start);
while (dict.size() > 0 && !words.isEmpty()) {
String word = words.pop();
int distance = distances.pop();
distance++;
for (int i = 0; i < wordLength; i++) {
char[] chars = word.toCharArray();
for (char c = 'a'; c <= 'z'; c++) {
chars[i] = c;
String newWord = new String(chars);
if (dict.contains(newWord)) {
if (newWord.equals(end)) {
return distance;
}
words.add(newWord);
distances.add(distance);
dict.remove(newWord);
}
}
}
}
return 0;
}
public int ladderLength1(String start, String end, Set<String> dict) {
if (dict.size() == 0) {
return 0;
}
int wordLength = start.length();
Queue<String> words = new LinkedList<String>();
words.add(start);
HashMap<String, Integer> visited = new HashMap<String, Integer>();
visited.put(start, 1);
Set<String> rest = new HashSet<String>();
rest.addAll(dict);
rest.remove(start);
while (!words.isEmpty()) {
String word = words.poll();
rest.remove(word);
for (String string : rest) {
if (isNeighbour(word, end, wordLength)) {
return visited.get(word) + 1;
}
if (isNeighbour(word, string, wordLength)) {
visited.put(string, visited.get(word) + 1);
words.add(string);
}
}
}
return 0;
}
private boolean isNeighbour(String word, String another, int wordLength) {
boolean flag = true;
char[] a = word.toCharArray();
char[] b = another.toCharArray();
for (int i = 0; i < wordLength; i++) {
if (a[i] == b[i]) {
continue;
}
if (flag == false) {
return flag;
} else {
flag = false;
}
}
return !flag;
}
public static void main(String[] args) {
// System.out.println(new WordLadder().isNeighbour("hot", "hit", 3));
String start = "a";
String end = "c";
String[] strings = new String[] { "a", "b", "c" };
Set<String> dict = new HashSet<String>();
dict.addAll(Arrays.asList(strings));
System.out.println(new WordLadder().ladderLength2(start, end, dict));
}
}