Advertisement
Guest User

Untitled

a guest
Dec 10th, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.20 KB | None | 0 0
  1. package com.interview.graph;
  2.  
  3. import sun.misc.Queue;
  4. import java.io.File;
  5. import java.io.FileNotFoundException;
  6. import java.util.*;
  7.  
  8. /**
  9.  * Created by mainuddin on 12/8/2016.
  10.  */
  11. public class Doublets {
  12.     static Set<String> dictionary = new HashSet<String>();
  13.     public static Stack<String> solve(String start, String end) {
  14.         HashSet<String> V = new HashSet<String>();
  15.         node s = new node();
  16.         s.c = start;
  17.         s.parent = null;
  18.         V.add(start);
  19.         Queue<node> Q = new Queue<>();
  20.         Q.enqueue(s);
  21.         Stack<String> res = null;
  22.         while (!Q.isEmpty()) {
  23.             node cur = null;
  24.             try {
  25.                 cur = Q.dequeue();
  26.             } catch (InterruptedException e) {
  27.                 e.printStackTrace();
  28.             }
  29.             if (cur.c.equals(end)) {
  30.                 res = new Stack<String>();
  31.                 while (cur != null) {
  32.                     res.push(cur.c);
  33.                     cur = cur.parent;
  34.                     }
  35.                 return res;
  36.                 } else {
  37.                 char[] S = cur.c.toCharArray();
  38.                 char[] ref = cur.c.toCharArray();
  39.                 for (int i = 0; i < S.length; i++) {
  40.                     for (int j = 0; j < 26; j++) {
  41.                         S[i] = (char) (j + 'a');
  42.                         String h = new String(S);
  43.                         if (S[i] != ref[i] && !V.contains(h) && dictionary.contains(h)) {
  44.                             V.add(h);
  45.                             node n = new node();
  46.                             n.c = h;
  47.                             n.parent = cur;
  48.                             Q.enqueue(n);
  49.                             }
  50.                         }
  51.                     S[i] = ref[i];
  52.                     }
  53.                 }
  54.             }
  55.         return res;
  56.         }
  57.     static class node {
  58.         String c;
  59.         node parent;
  60.     }
  61.     public static void main(String[] args) {
  62.  
  63.         Scanner file = null;
  64.         try {
  65.             file = new Scanner(new File("E:\\L2-T2\\interview-master\\src\\com\\interview\\graph\\dictionary.txt"));
  66.         } catch (FileNotFoundException e) {
  67.             e.printStackTrace();
  68.         }
  69.        // int counter = 0;
  70.         while (file.hasNext()) {
  71.             dictionary.add(file.next().trim().toLowerCase());
  72.          //   counter++;
  73.         }
  74.         Scanner in = new Scanner(System.in);
  75.         int test_cases = 0;
  76.         String s1 = new String();
  77.         String s2 = new String();
  78.         test_cases = in.nextInt();
  79.         //System.out.println(counter+","+recounter);
  80.         while(true) {
  81.             Scanner str = new Scanner(System.in);
  82.             s1 = str.nextLine().toLowerCase();
  83.  
  84.             s2 = str.nextLine().toLowerCase();
  85.             Stack<String> sol = solve(s1, s2);
  86.             if (sol != null) {
  87.                 while (!sol.isEmpty()) {
  88.                     System.out.print(sol.pop()+"->");
  89.                 }
  90.                 System.out.println();
  91.             } else if (sol == null || sol.isEmpty()) {
  92.                 System.out.println("No solution.");
  93.             }
  94.             test_cases--;
  95.             if (test_cases == 0) break;
  96.         }
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement