-
Notifications
You must be signed in to change notification settings - Fork 0
/
WorldLadder.java
103 lines (84 loc) · 2.06 KB
/
WorldLadder.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
import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashSet;
import java.util.Scanner;
public class WorldLadder {
private static HashSet<String> hs = new HashSet<String>();
private static HashSet<String> nhs = new HashSet<String>();
private static HashSet<String> lhs = new HashSet<String>();
public static void main(String[] args) throws FileNotFoundException {
Scanner scan = new Scanner(new File("src/ladderInput.txt"));
AdvCSSeanStack stringstack = new AdvCSStack();
loadDict();
while (scan.hasNextLine())
{
String s = scan.nextLine();
Scanner dan = new Scanner(s);
stringstack = findLadder(dan.next(), dan.next());
while (!stringstack.isEmpty())
{
System.out.print(stringstack.pop());
}
}
}
public static void loadDict() throws FileNotFoundException {
Scanner plan = new Scanner(new File("src/dictionary.txt"));
while (plan.hasNextLine())
{
hs.add(plan.nextLine());
}
}
public static AdvCSStack findLadder(String start, String end) throws FileNotFoundException
{
AdvCSQueue<AdvCSStack> queue = new AdvCSQueue<AdvCSStack>();
AdvCSSStack<String> stack = new AdvCSStack<String>();
for (String s: hs)
{
if (s.length() == start.length())
nhs.add(s);
}
for (String s: nhs)
{
if (oneLetterCheck(start, s))
{
AdvCSStack<String> stacktwo = stack.copy();
stacktwo.push(s);
queue.enqueue(stacktwo);
lhs.add(s);
}
}
while (!queue.isEmpty())
{
stack = queue.dequeue();
for (String s: nhs)
{
if (oneLetterCheck(start, s))
{
AdvCSStack<String> stacktwo = stack.copy();
stacktwo.push(s);
queue.enqueue(stacktwo);
lhs.add(s);
}
}
}
return queue.dequeue();
}
public static boolean oneLetterCheck(String str, String strtwo)
{
int length = str.length();
int count = 0;
for (int i = 0; i < length; i++)
{
if (str.charAt(i) == strtwo.charAt(i))
{
count++;
}
}
if (count == str.length() - 1 && !(lhs.contains(strtwo)))
{
return true;
}
else
return false;
}
}