Advertisement
unknown_0711

Untitled

Jan 4th, 2023
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.48 KB | None | 0 0
  1. class Solution {
  2. public List<List<String>> findLadders(String beginWord, String endWord, List<String> wordList) {
  3. if(!wordList.contains(endWord)) {
  4. return new LinkedList<>();
  5. }
  6.  
  7. // build a dictionary of one edit words
  8. Map<String, Set<String>> dictionary = buildDictionary(beginWord, wordList);
  9.  
  10. // bfs
  11. Map<String, Set<String>> graph = buildLadderGraph(beginWord, endWord, wordList, dictionary);
  12.  
  13. // dfs all paths
  14. return traceAllPaths(graph, endWord, beginWord);
  15. }
  16.  
  17. private Map<String, Set<String>> buildDictionary(String beginWord,List<String> wordList) {
  18. int wordLen = beginWord.length();
  19. Map<String, Set<String>> dict = new HashMap<>();
  20. for(String word : wordList) {
  21. for (int i = 0; i < wordLen; i++) {
  22. String key = word.substring(0, i) + "-" + word.substring(i + 1, wordLen);
  23. dict.putIfAbsent(key, new HashSet<>());
  24. dict.get(key).add(word);
  25. }
  26. }
  27. return dict;
  28. }
  29.  
  30. private Map<String, Set<String>> buildLadderGraph(String beginWord, String endWord, List<String> wordList, Map<String, Set<String>> dict) {
  31. Queue<String> queue = new LinkedList<>();
  32. queue.offer(beginWord);
  33.  
  34. Set<String> visited = new HashSet<>();
  35. visited.add(beginWord);
  36.  
  37. Map<String, Set<String>> graph = new HashMap<>();
  38.  
  39. while(!queue.isEmpty()) {
  40. int size = queue.size();
  41. Set<String> levelVisits = new HashSet<>();
  42. for(int i = 0; i < size; i++) {
  43. String word = queue.poll();
  44.  
  45. if(word.equals(endWord)) {
  46. continue;
  47. }
  48.  
  49. graph.putIfAbsent(word, new HashSet<>());
  50.  
  51. int wordLen = beginWord.length();
  52. for (int j = 0; j < wordLen; j++) {
  53. String key = word.substring(0, j) + "-" + word.substring(j + 1, wordLen);
  54. if(!dict.containsKey(key)) {
  55. continue;
  56. }
  57.  
  58. for (String w : dict.get(key)) {
  59. if(!visited.contains(w)) {
  60. levelVisits.add(w);
  61. queue.offer(w);
  62. graph.get(word).add(w);
  63. }
  64. }
  65. }
  66. }
  67.  
  68. for(String word : levelVisits) {
  69. visited.add(word);
  70. }
  71. }
  72.  
  73. return graph;
  74. }
  75.  
  76. private List<List<String>> traceAllPaths(Map<String, Set<String>> graph, String endWord, String node) {
  77. List<List<String>> result = new ArrayList<>();
  78. if(node.equals(endWord)) {
  79. result.add(new ArrayList<>());
  80. result.get(0).add(node);
  81. return result;
  82. }
  83.  
  84. if(!graph.containsKey(node) || graph.get(node).size() == 0) {
  85. return result;
  86. }
  87.  
  88. for(String child : graph.get(node)) {
  89. List<List<String>> childRes = traceAllPaths(graph, endWord, child);
  90. for(List<String> list : childRes) {
  91. list.add(0, node);
  92. result.add(list);
  93. }
  94. }
  95.  
  96. return result;
  97. }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement