-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWordLadder1.java
More file actions
98 lines (91 loc) · 2.83 KB
/
WordLadder1.java
File metadata and controls
98 lines (91 loc) · 2.83 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
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
public class WordLadder1 {
public static void main(String[] args) {
WordLadder1 wl = new WordLadder1();
String[] wordlist = { "poon", "plee", "same", "poie", "plea", "plie", "poin" };
int ans = wl.wordLadderLength("toon", "plea", wordlist);
System.out.println(ans);
}
public int wordLadderLength(String startWord, String targetWord,
String[] wordList) {
boolean contains = false;
for (String w : wordList) {
if (w.equals(targetWord)) {
contains = true;
break;
}
}
if (!contains)
return 0;
wordList = addWord(startWord, wordList);
List<List<Integer>> edges = new ArrayList<>();
collectEdges(edges, wordList);
int startWordIdx = 0;
int targetWordIdx = 0;
for (int i = 0; i < wordList.length; i++) {
if (wordList[i].equals(startWord))
startWordIdx = i;
if (wordList[i].equals(targetWord))
targetWordIdx = i;
}
class Node {
int wordIdx;
int distance;
public Node(int wordIdx, int distance) {
this.wordIdx = wordIdx;
this.distance = distance;
}
}
Queue<Node> queue = new LinkedList<>();
boolean[] addedInQ = new boolean[wordList.length];
queue.add(new Node(startWordIdx, 1));
addedInQ[startWordIdx] = true;
while (!queue.isEmpty()) {
Node currentNode = queue.poll();
if (currentNode.wordIdx == targetWordIdx)
return currentNode.distance;
for (List<Integer> edge : edges) {
if (!addedInQ[edge.get(1)] && edge.get(0) == currentNode.wordIdx) {
queue.offer(new Node(edge.get(1), currentNode.distance + 1));
addedInQ[edge.get(1)] = true;
}
}
}
return 0;
}
private void collectEdges(List<List<Integer>> edges, String[] wordlist) {
for (int i = 0; i < wordlist.length; i++) {
for (int j = 0; j < wordlist.length; j++) {
if (i != j && isConnection(wordlist[i], wordlist[j])) {
List<Integer> connection = new ArrayList<>(2);
connection.add(i);
connection.add(j);
edges.add(connection);
}
}
}
}
private boolean isConnection(String word1, String word2) {
int requiredSameTextCount = word1.length() - 1;
for (int i = 0; i < word1.length(); i++) {
if (word1.charAt(i) == word2.charAt(i))
requiredSameTextCount--;
}
return requiredSameTextCount <= 0;
}
private String[] addWord(String word, String[] wordList) {
for (String wString : wordList) {
if (wString.equals(word))
return wordList;
}
String[] newList = new String[wordList.length + 1];
newList[0] = word;
for (int i = 0; i < wordList.length; i++) {
newList[i + 1] = wordList[i];
}
return newList;
}
}