-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcodinter18.10.java
63 lines (63 loc) · 1.75 KB
/
codinter18.10.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
import java.util.*;
public class Main {
public static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
String[] words = {"maps", "tan", "tree", "apple", "cans", "help", "aped", "free", "apes", "flat", "trap", "fret", "trip", "trie", "frat", "fril"};
HashSet<String> dict = new HashSet<String>();
for(String s:words){
dict.add(s);
}
ArrayList<String> list = getAns("tree", "flat", dict);
for (String word : list) {
System.out.println(word);
}
}
public static ArrayList <String> getAns(String st,String en,
HashSet<String>wordDict){
HashMap <String, String> backtrack=new HashMap<String,String> ();
Queue <String> q=new LinkedList<String> ();
HashSet <String>Visited=new HashSet <String> ();
q.add(st);
Visited.add(st);
while(!q.isEmpty()){
String now=q.poll();
ArrayList <String> oneEditedWords=getoew(now,wordDict);
for(String s:oneEditedWords){
if(s.equals(en)){ //不能用==来判断字符串是否相等
ArrayList <String> ans=new ArrayList<String> ();
ans.add(s);
backtrack.put(s,now);
String fr=s;
while(backtrack.get(fr)!=null){ //
fr=backtrack.get(fr);
ans.add(0,fr);
}
return ans;
}
else{
if(!Visited.contains(s)){
q.add(s);
Visited.add(s);
backtrack.put(s,now);
}
}
}
}
return null;
}
public static ArrayList <String> getoew(String now,HashSet<String> wordDict){
char chArray[]=now.toCharArray();
ArrayList <String> al=new ArrayList<String> ();
for(int i=0;i<now.length();i++){
for(char ch='a';ch<='z';ch++){
chArray[i]=ch;
String str=new String(chArray);
if(ch!=now.charAt(i)&&wordDict.contains(str)){
al.add(str);
}
chArray[i]=now.charAt(i);
}
}
return al;
}
}