-
Notifications
You must be signed in to change notification settings - Fork 0
/
WordLadder-I.java
89 lines (63 loc) · 2.42 KB
/
WordLadder-I.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
/*
Given two words A and B, and a dictionary, C, find the length of shortest transformation sequence from A to B, such that:
You must change exactly one character in every transformation.
Each intermediate word must exist in the dictionary.
Note:
Return 0 if there is no such transformation sequence.
All words have the same length.
All words contain only lowercase alphabetic characters.
Input Format:
The first argument of input contains a string, A.
The second argument of input contains a string, B.
The third argument of input contains an array of strings, C.
Output Format:
Return an integer representing the minimum number of steps required to change string A to string B.
Constraints:
1 <= length(A), length(B), length(C[i]) <= 25
1 <= length(C) <= 5e3
Example :
Input 1:
A = "hit"
B = "cog"
C = ["hot", "dot", "dog", "lot", "log"]
Output 1:
5
Explanation 1:
"hit" -> "hot" -> "dot" -> "dog" -> "cog"
*/
public class Solution {
static char[] ch= {'a','b','c','d','e','f','g','h','i','j','k','l','m','n'
,'o','p','q','r','s','t','u','v','w','x','y','z'};
public int solve(String A, String B, ArrayList<String> C) {
HashSet<String> dictionary = new HashSet<>(C);
HashSet<String> visited = new HashSet<>();
visited.add(A);
Queue<String> queue = new LinkedList<>();
queue.add(A);
int changes = 1;
while(queue.size() > 0){
int size = queue.size();
for(int i=1; i<=size; i++){
String s = queue.poll();
if(s.equals(B)){
return changes;
}
else{
StringBuilder sb = new StringBuilder(s);
for(int j=0; j<s.length(); j++){
for(int k=0; k<26; k++){
sb.setCharAt(j,ch[k]);
String s1 = sb.toString();
if(dictionary.contains(s1) && !visited.contains(s1)){
queue.add(s1);
visited.add(s1);
}
}
}
}
}
changes++;
}
return 0;
}
}