-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWordLadderSolver.java
100 lines (86 loc) · 2.65 KB
/
WordLadderSolver.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
/**
* Object that solves word ladder
* Solves EE422C programming assignment #4
* @authors Sneha Vasantharao, Jai Bock Lee
* @version 1.1 2016-3-1
*
* UTEID: sv8398, jbl932
* Lab Section: 11-12:30pm, Lisa Hua
*
*/
package assignment4;
import java.util.List;
import java.util.ArrayList;
import java.util.HashMap;
// do not change class name or interface it implements
public class WordLadderSolver implements Assignment4Interface
{
// delcare class members here.
HashMap<Character, ArrayList<String>> originalWordList;
int numberOfWords;
boolean[][] dictionaryMatrix;
// add a constructor for this object. HINT: it would be a good idea to set up the dictionary there
public WordLadderSolver (HashMap<Character, ArrayList<String>> dictionary)
{
originalWordList = dictionary;
int originalWordListSize = 0;
for (ArrayList<String> list : originalWordList.values())
{
originalWordListSize += list.size();
}
dictionaryMatrix = new boolean[originalWordListSize][originalWordListSize];
Long start = System.nanoTime();
for (ArrayList<String> list : originalWordList.values())
{
for (String word1 : list)
{
for(String word2 : list)
{
if(checkNextTo(word1,word2))
{
addEdge(list.indexOf(word1),list.indexOf(word2));
}
}
}
}
Long stop = System.nanoTime();
double time = ((double)stop - (double)start)/1000000;
System.out.println("Graph execution time: " + time + " ms");
}
// do not change signature of the method implemented from the interface
@Override
public List<String> computeLadder(String startWord, String endWord) throws NoSuchLadderException
{
// implement this method
throw new UnsupportedOperationException("Not implemented yet!");
}
@Override
public boolean validateResult(String startWord, String endWord, List<String> wordLadder)
{
throw new UnsupportedOperationException("Not implemented yet!");
}
// add additional methods here
boolean checkEdge(int p, int q)
{
return dictionaryMatrix[p][q];
}
void addEdge(int p, int q)
{
dictionaryMatrix[p][q] = true;
}
void removeEdge(int p, int q)
{
dictionaryMatrix[p][q] = false;
}
public static boolean checkNextTo(String a, String b)
{
assert a.length() == b.length();
int differ = 0;
for (int i = 0; i < a.length(); i++)
{
if (a.charAt(i) != b.charAt(i)) differ++;
if (differ > 1) return false;
}
return true;
}
}