Advertisement
rishu110067

Untitled

Feb 24th, 2022
918
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.78 KB | None | 0 0
  1. static ArrayList<String> string_transformation(ArrayList<String> words, String start, String stop) {
  2.     if(start == null || (start.equals(stop) && words.size() == 0))
  3.         return new ArrayList<String>(Arrays.asList("-1"));
  4.    
  5.     HashSet<String> dict = new HashSet<String>(words);
  6.     dict.add(start);
  7.     dict.add(stop);
  8.    
  9.     // Edge case
  10.     if(start.equals(stop))
  11.     {
  12.         ArrayList<String> result = new ArrayList<String>();
  13.         result.add(start);
  14.         for(String w: dict){
  15.             if(diffOfOne(w, start)){
  16.                 result.add(w);
  17.                 break;
  18.             }
  19.         }
  20.         result.add(stop);
  21.         return result;
  22.     }
  23.  
  24.     if(dict.size() < start.length()*26) {  
  25.         return bfsWithCompare(dict, start, stop);
  26.     }
  27.     return bfsWithTransform(dict, start, stop );
  28. }
  29.  
  30.  
  31.  
  32. static ArrayList<String> bfsWithCompare(HashSet<String> dict, String start, String stop){
  33.  
  34.     Queue<String> q = new LinkedList<String>();
  35.  
  36.     HashMap<String, String> parent = new HashMap<String, String>();
  37.  
  38.     Set<String> visited = new HashSet();
  39.  
  40.     ArrayList<String> result = new ArrayList<String>();
  41.  
  42.     q.add(start);
  43.  
  44.     visited.add(start);
  45.  
  46.     parent.put(start, null);
  47.  
  48.    
  49.  
  50.     while(!q.isEmpty()){
  51.  
  52.        int size = q.size();
  53.  
  54.         for(int i = 0; i<size ;i++){
  55.  
  56.             String word = q.poll();
  57.  
  58.             if( word.equals(stop)) return createResultList(stop, parent);
  59.  
  60.             for(String w: dict){
  61.  
  62.                 if( !visited.contains(w) && diffOfOne(w, word)){
  63.  
  64.                     q.add(w);
  65.  
  66.                     visited.add(w);
  67.  
  68.                     parent.put(w,word);
  69.  
  70.                 }
  71.  
  72.             }
  73.  
  74.         }
  75.  
  76.     }
  77.  
  78.     return new ArrayList<>(Arrays.asList("-1"));
  79.  
  80. }
  81.  
  82.  
  83.  
  84. static ArrayList<String> bfsWithTransform(HashSet<String> dict, String start, String stop){
  85.  
  86.     Queue<String> q = new LinkedList<String>();
  87.  
  88.     HashMap<String, String> parent = new HashMap<String, String>();
  89.  
  90.     Set<String> visited = new HashSet();
  91.  
  92.     ArrayList<String> result = new ArrayList<String>();
  93.  
  94.     q.add(start);
  95.  
  96.     visited.add(start);
  97.  
  98.     parent.put(start, null);
  99.  
  100.    
  101.  
  102.     if(start.equals(stop)) visited.remove(start);
  103.  
  104.     while(!q.isEmpty()){
  105.  
  106.         int size = q.size();
  107.  
  108.        
  109.  
  110.         for(int i = 0; i<size ;i++){
  111.  
  112.             String word = q.poll();
  113.  
  114.             if(word.equals(stop)) return createResultList(stop, parent);
  115.  
  116.            
  117.  
  118.             char[] arr = word.toCharArray();
  119.  
  120.             for(int j = 0; j<arr.length; j++){
  121.  
  122.                 char original = arr[j];
  123.  
  124.                 for( char ch ='a'; ch <= 'z' ; ch++){
  125.  
  126.                     if(ch == original ) continue;
  127.  
  128.                     arr[j] = ch;
  129.  
  130.                     String s = new String(arr);
  131.  
  132.                     if(!visited.contains(s) && dict.contains(s)){
  133.  
  134.                         q.add(s);
  135.  
  136.                         parent.put(s,word);
  137.  
  138.                         visited.add(s);
  139.  
  140.                     }
  141.  
  142.                 }
  143.  
  144.                 arr[j] = original;
  145.  
  146.             }
  147.  
  148.         }
  149.  
  150.     }
  151.  
  152.     return new ArrayList<>(Arrays.asList("-1"));
  153.  
  154. }
  155.  
  156.  
  157.  
  158. static boolean diffOfOne(String w1, String w2){
  159.  
  160.     int diff = 0;
  161.  
  162.     for(int i =0; i< w1.length() ;i++){
  163.  
  164.         if(w1.charAt(i) != w2.charAt(i)) diff++;
  165.  
  166.         if(diff>1) return false;
  167.  
  168.     }
  169.  
  170.     return true;
  171.  
  172. }
  173.  
  174.  
  175.  
  176.     static ArrayList<String> createResultList(String stop, HashMap<String, String> parentMap){
  177.  
  178.     ArrayList<String> result = new ArrayList<String>();
  179.  
  180.     result.add(stop);
  181.  
  182.     String parent = parentMap.get(stop);
  183.  
  184.     while(parent != null){
  185.  
  186.         result.add(parent);
  187.  
  188.         parent = parentMap.get(parent);
  189.  
  190.     }
  191.  
  192.     Collections.reverse(result);
  193.  
  194.     return result;
  195.  
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement