Advertisement
lifeiteng

126. Word Ladder II

Feb 1st, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.56 KB | None | 0 0
  1. class Solution {
  2.    
  3.     List<List<String>> re = new ArrayList<>();
  4.    
  5.     Map<String, Integer> distance = new HashMap<>();
  6.     Map<String, List<String>> map = new HashMap<>();
  7.    
  8.     public List<List<String>> findLadders(String beginWord, String endWord, List<String> wordList) {
  9.         distance.put(beginWord, 0);
  10.         for(String str : wordList) map.put(str, new ArrayList<>());
  11.         if(!bfs(beginWord, endWord, new HashSet<>(wordList))) return re;
  12.         List<String> list = new ArrayList<>();
  13.         dfs(list, endWord, beginWord);
  14.         return re;
  15.     }
  16.    
  17.     void dfs(List<String> path, String crt, String start)
  18.     {
  19.         path.add(crt);
  20.         System.out.println(path);
  21.         if (crt.equals(start))
  22.         {
  23.             Collections.reverse(path);
  24.             re.add(new ArrayList<String>(path));
  25.             Collections.reverse(path);
  26.         }
  27.         else
  28.         {
  29.             for (String next : map.get(crt))
  30.             {
  31.                 if (distance.containsKey(next) && distance.get(crt) == distance.get(next) + 1)
  32.                 {
  33.                     dfs(path, next, start);
  34.                 }
  35.             }          
  36.         }
  37.         path.remove(path.size() - 1);
  38.     }    
  39.  
  40.     boolean bfs(String a, String b, Set<String> set)
  41.     {
  42.         boolean found = false;
  43.         Queue<String> q = new LinkedList<>();
  44.         q.offer(a);
  45.        
  46.         while(!q.isEmpty())
  47.         {
  48.             String cur = q.poll();
  49.             if(cur.equals(b))
  50.             {
  51.                 found = true;
  52.             }
  53.             for(String next : expand(cur, set))
  54.             {
  55.                 map.get(next).add(cur);
  56.                 if(!distance.containsKey(next))
  57.                 {
  58.                     distance.put(next, distance.getOrDefault(cur, 0) + 1);
  59.                     q.offer(next);
  60.                 }
  61.  
  62.             }
  63.         }
  64.         return found;
  65.     }
  66.    
  67.     List<String> expand(String crt, Set<String> dict)
  68.     {
  69.        
  70.         List<String> expansion = new ArrayList<String>();
  71.  
  72.         for (int i = 0; i < crt.length(); i++)
  73.         {
  74.             for (char ch = 'a'; ch <= 'z'; ch++)
  75.             {
  76.                 if (ch != crt.charAt(i))
  77.                 {
  78.                     String expanded = crt.substring(0, i) + ch + crt.substring(i + 1);
  79.                     if (dict.contains(expanded))
  80.                     {
  81.                         expansion.add(expanded);
  82.                     }
  83.                 }
  84.             }
  85.         }
  86.  
  87.         return expansion;
  88.     }
  89.  
  90.    
  91.    
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement