word to word

Question:

For each word, you can get a list of neighbor words by calling getWords(String), find all the paths from word1 to word2.

 public class Solution {
List<List<String>> finalList = new ArrayList<>(); public static void main(String[] args) {
Solution s = new Solution();
System.out.println(s.getPath("face", "book"));
} public List<List<String>> getPath(String s, String e) {
List<String> list = new ArrayList<String>();
helper(s, e, list, new HashSet<String>());
return finalList;
} public void helper(String s, String e, List<String> list, Set<String> set) {
if (set.contains(s) || (list.size() != && list.get(list.size() - ).equals(e))) return;
list.add(s);
set.add(s); if (s.equals(e)) {
finalList.add(new ArrayList<String>(list));
} List<String> dicts = getWords(s);
for (String str : dicts) {
helper(str, e, list, set);
} list.remove(s);
set.remove(s);
} public List<String> getWords(String str) {
List<String> list1 = new ArrayList<String>(); if (str.equals("face")) {
list1.add("foce");
list1.add("fack");
} else if (str.equals("foce")) {
list1.add("face");
list1.add("fook");
} else if (str.equals("fack")) {
list1.add("fook");
list1.add("faok");
} else if (str.equals("fook")) {
list1.add("fack");
list1.add("book");
} else if (str.equals("faok")) {
list1.add("foce");
list1.add("book");
}
return list1;
}
}
上一篇:TypeScript完全解读(26课时)_18.Mixins混入


下一篇:Playmaker Input篇教程之引入的核心概念