-
Notifications
You must be signed in to change notification settings - Fork 0
/
WorldLadderSean.java
106 lines (87 loc) · 2.47 KB
/
WorldLadderSean.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
import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashSet;
import java.util.Scanner;
public class WorldLadderSean {
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 AdvCSSeanStack();
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>();
AdvCSStack<String> stack = new AdvCSStack<String>();
AdvCSStack<String> stacktwo = new AdvCSStack<String>();
String str = "";
for (String s: hs)
{
if (s.length() == start.length())
lengthhs.add(s);
}
for (String s: lengthhs)
{
if (oneLetterCheck(start, s))
{
stacktwo = stack.copy();
stacktwo.push(s);
queue.enqueue(stacktwo);
usedhs.add(s);
}
}
while (!queue.isEmpty())
{
stack = queue.dequeue();
usedhs.add(stack.peek());
for (String s: lengthhs)
{
str = (String) queue.peek().peek();
if (oneLetterCheck(str, s))
{
stacktwo = stack.copy();
stacktwo.push(s);
queue.enqueue(stacktwo);
}
}
}
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;
}
}