|
| 1 | +import javafx.util.Pair; |
| 2 | + |
| 3 | +import java.util.*; |
| 4 | + |
| 5 | +class LadderLength { |
| 6 | + public int ladderLength(String beginWord, String endWord, List<String> wordList) { |
| 7 | + |
| 8 | + // Since all words are of same length. |
| 9 | + int L = beginWord.length(); |
| 10 | + |
| 11 | + // Dictionary to hold combination of words that can be formed, |
| 12 | + // from any given word. By changing one letter at a time. |
| 13 | + Map<String, List<String>> allComboDict = new HashMap<>(); |
| 14 | + |
| 15 | + wordList.forEach( |
| 16 | + word -> { |
| 17 | + for (int i = 0; i < L; i++) { |
| 18 | + // Key is the generic word |
| 19 | + // Value is a list of words which have the same intermediate generic word. |
| 20 | + String newWord = word.substring(0, i) + '*' + word.substring(i + 1, L); |
| 21 | + List<String> transformations = allComboDict.getOrDefault(newWord, new ArrayList<>()); |
| 22 | + transformations.add(word); |
| 23 | + allComboDict.put(newWord, transformations); |
| 24 | + } |
| 25 | + }); |
| 26 | + |
| 27 | + // Queue for BFS |
| 28 | + Queue<Pair<String, Integer>> Q = new LinkedList<>(); |
| 29 | + Q.add(new Pair(beginWord, 1)); |
| 30 | + |
| 31 | + // Visited to make sure we don't repeat processing same word. |
| 32 | + Map<String, Boolean> visited = new HashMap<>(); |
| 33 | + visited.put(beginWord, true); |
| 34 | + |
| 35 | + while (!Q.isEmpty()) { |
| 36 | + Pair<String, Integer> node = Q.remove(); |
| 37 | + String word = node.getKey(); |
| 38 | + int level = node.getValue(); |
| 39 | + for (int i = 0; i < L; i++) { |
| 40 | + |
| 41 | + // Intermediate words for current word |
| 42 | + String newWord = word.substring(0, i) + '*' + word.substring(i + 1, L); |
| 43 | + |
| 44 | + // Next states are all the words which share the same intermediate state. |
| 45 | + for (String adjacentWord : allComboDict.getOrDefault(newWord, new ArrayList<>())) { |
| 46 | + // If at any point if we find what we are looking for |
| 47 | + // i.e. the end word - we can return with the answer. |
| 48 | + if (adjacentWord.equals(endWord)) { |
| 49 | + return level + 1; |
| 50 | + } |
| 51 | + // Otherwise, add it to the BFS Queue. Also mark it visited |
| 52 | + if (!visited.containsKey(adjacentWord)) { |
| 53 | + visited.put(adjacentWord, true); |
| 54 | + Q.add(new Pair(adjacentWord, level + 1)); |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + return 0; |
| 61 | + } |
| 62 | +} |
0 commit comments