Advertisement
Guest User

Untitled

a guest
Jul 17th, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.16 KB | None | 0 0
  1. import javafx.util.Pair;
  2. import org.testng.annotations.Test;
  3.  
  4. import java.util.*;
  5. import static org.testng.Assert.*;
  6.  
  7. public class Sol {
  8.  
  9. static Set<Map<Integer, Integer>> indexPointers(String s) {
  10. Set<Map<Integer, Integer>> result = new HashSet<>();
  11. for (int i = 0; i < s.length(); i++) {
  12. Map<Integer, Integer> indexPointer = new TreeMap<Integer, Integer>();
  13. for (int j = 0; j < s.length(); j++) {
  14. if (j != i) {
  15. indexPointer.put(j, s.codePointAt(j));
  16. }
  17. }
  18. result.add(indexPointer);
  19. }
  20. return result;
  21. }
  22.  
  23. public static boolean hasPath(List<String> words, String first, String last) {
  24. Map<Map<Integer, Integer>, Set<String>> index = new HashMap<>();
  25.  
  26. Set<String> wordsSet = new HashSet<>(words);
  27. if (!wordsSet.contains(first)) return false;
  28. if (!wordsSet.contains(last)) return false;
  29.  
  30. for (String word : words) {
  31. for (Map<Integer, Integer> indexPointer : indexPointers(word)) {
  32. Set<String> indexWords = index.getOrDefault(indexPointer, new HashSet<>());
  33. indexWords.add(word);
  34. index.put(indexPointer, indexWords);
  35. }
  36. }
  37.  
  38. Deque<String> found = new LinkedList<>();
  39. found.addFirst(first);
  40.  
  41. while (found.size() > 0) {
  42. String word = found.removeFirst();
  43.  
  44. wordsSet.remove(word);
  45. for (Map<Integer, Integer> indexPointer : indexPointers(word)) {
  46. Set<String> nextWords = index.get(indexPointer);
  47. for (String nextWord : nextWords) {
  48. if (nextWord.equals(last)) return true;
  49. if (wordsSet.contains(nextWord)) {
  50. found.addLast(nextWord);
  51. }
  52. }
  53. }
  54. }
  55.  
  56. return false;
  57. }
  58.  
  59. static int editDistance(String s1, String s2) {
  60. int result = 0;
  61. for (int i = 0; i < Math.min(s1.length(), s2.length()); i++) {
  62. result += s1.codePointAt(i) == s2.codePointAt(i) ? 0 : 1;
  63. }
  64. return result;
  65. }
  66.  
  67. public static int hasPath2(List<String> words, String first, String last) {
  68. Set<String> wordsSet = new HashSet<>(words);
  69. if (!wordsSet.contains(first)) return -1;
  70. if (!wordsSet.contains(last)) return -1;
  71.  
  72. Deque<Pair<String, Integer>> found = new LinkedList<>();
  73. found.addFirst(new Pair(first, 0));
  74.  
  75. while (found.size() > 0) {
  76. Pair<String, Integer> elem = found.removeFirst();
  77. String word = elem.getKey();
  78. Integer distance = elem.getValue();
  79. wordsSet.remove(word);
  80. for (String next : wordsSet) {
  81. if (editDistance(word, next) == 1) {
  82. if (next.equals(last)) return distance + 1;
  83. found.addLast(new Pair<>(next, distance + 1));
  84. }
  85. }
  86. }
  87.  
  88. return -1;
  89. }
  90.  
  91. @Test
  92. public static void checkHasPath() {
  93. List<String> dict = Arrays.asList(
  94. "BARE",
  95. "CARE",
  96. "CASE",
  97. "CASK",
  98. "BASK",
  99. "RATE",
  100. "MATE",
  101. "MANE",
  102. "BANE",
  103. "BONE",
  104. "XYZF"
  105. );
  106. assertTrue(hasPath(dict, "BARE", "BASK"));
  107. // assertTrue(hasPath2(dict, "BARE", "BASK"));
  108. assertTrue(hasPath(dict, "BASK", "BARE"));
  109. assertEquals(hasPath2(dict, "BASK", "BARE"), 4);
  110. assertFalse(hasPath(dict, "BARE", "XYZF"));
  111. // assertFalse(hasPath2(dict, "BARE", "XYZF"));
  112.  
  113. }
  114.  
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement