-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWordLadder.java
164 lines (142 loc) · 4.12 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
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
import java.util.*;
import java.io.*;
public class WordLadder {
public static HashSet<String> h = new HashSet<>();
public static AdvCSQueue<AdvCSStack> wordSims = new AdvCSQueue<>();
public static HashSet<String> visited = new HashSet<>();
public static AdvCSStack<String> wordList = new AdvCSStack<>();
public static HashSet<String> optimized = new HashSet<>();
public static void main(String[] args) throws FileNotFoundException {
Scanner fs = new Scanner(new File("src/dictionary.txt"));
Scanner ls = new Scanner(fs.nextLine());
String[] input = new String[2];
String s;
AdvCSStack<String> wordList = null;
loadDict();
System.out.println(h.size());
// while(fs.hasNextLine()) {
// s = fs.nextLine();
// //input =getStartEnd(s, input);
//
// wordList.push(input[0]);
// wordSims.enqueue(wordList);
// s = findLadder(input [0], input[1]);
// }
// input[0] = "dears";
// input[1] = "fears";
s = findLadder("stone", "money");
System.out.println(s);
}
public static String findLadder(String start, String end) {
optimized = optimize(h, start, end);
AdvCSStack<String> holder = new AdvCSStack<>();
holder.push(start);
visited.add(start);
wordSims.enqueue(holder);
while (!wordSims.isEmpty()) {
holder = wordSims.dequeue();
String top = holder.peek();
visited.add(holder.peek());
if (holder.peek().equals(end)) {
String result = "";
while (!holder.isEmpty()) {
result = holder.pop() + " " + result + " ";
}
return result;
} else if (!holder.peek().equals(end)) {
int startLength = top.length();
// holder = wordSims.dequeue();
int differences = 0;
// Iterator<String> iterator = optimized.iterator();
// for(int i = 0; i < h.size(); i ++) {
// while(iterator.hasNext()) {
for (String s : h) {
// String holderString = (String) iterator.next();
String holderString = s;
if (holderString.length() == top.length()) {
differences = 0;
for (int j = 0; j < startLength; j++) {
if (!top.substring(j, j + 1).equals(holderString.substring(j, j + 1))) {
differences = differences + 1;
}
}
if (differences == 1 && !visited.contains(holderString)) {
AdvCSStack<String> newList = new AdvCSStack<>();
newList = copyStack(holder);
newList.push(holderString);
wordSims.enqueue(newList);
}
else {
continue;
}
}
}
} else {
continue;
}
}
System.out.println("nope");
return null;
// int startLength = start.length();
// holder = wordSims.dequeue();
// int differences = 0;
// for(int i = 0; i < h.size(); i ++) {
// String holderString = ((List<String>) h).get(i);
// for(int j = 0; j < startLength; j ++) {
// if (!start.substring(i, i + 1).equals(holderString.substring(i, i + 1))) {
// differences ++;
// }
// }
// if(differences == 1) {
//
// }
// }
//
//
//
// return null;
}
public static AdvCSStack copyStack(AdvCSStack<String> inStack) {
AdvCSStack<String> holder = new AdvCSStack<>();
AdvCSStack<String> finalOutput = new AdvCSStack<>();
while (!inStack.isEmpty()) {
holder.push(inStack.pop());
}
while (!holder.isEmpty()) {
finalOutput.push(holder.peek());
inStack.push(holder.pop());
}
return finalOutput;
}
public static String[] getStartEnd(String inString, String[] input) {
// int space = inString.indexOf(" ");
// input[0] = inString.substring(0, space - 1);
// input[1] = inString.substring(space);
// return input;
input = inString.split("\\s+");
return input;
}
public static void loadDict() throws FileNotFoundException {
Scanner fs = new Scanner(new File("src/dictionary.txt"));
while (fs.hasNextLine()) {
h.add(fs.nextLine());
}
}
public static HashSet optimize(HashSet inH, String start, String end) {
Iterator<String> iterator = h.iterator();
String holder = "";
for (String s : h) {
if (s.length() == start.length()) {
optimized.add(s);
}
}
return optimized;
// while(iterator.hasNext()) {
// holder = iterator.next();
// if(holder.length() == start.length()) {
// optimized.add(holder);
// }
// }
// return optimized;
}
}