| 
 | 1 | +package com.leetcode.graphs;  | 
 | 2 | + | 
 | 3 | + | 
 | 4 | +import javafx.util.Pair;  | 
 | 5 | + | 
 | 6 | +import java.util.*;  | 
 | 7 | + | 
 | 8 | +import static org.junit.jupiter.api.Assertions.assertEquals;  | 
 | 9 | + | 
 | 10 | +/**  | 
 | 11 | + * Level: Medium  | 
 | 12 | + * Link: https://leetcode.com/problems/word-ladder/  | 
 | 13 | + * Description:  | 
 | 14 | + * Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest transformation  | 
 | 15 | + * sequence from beginWord to endWord, such that:  | 
 | 16 | + * <p>  | 
 | 17 | + * Only one letter can be changed at a time. Each transformed word must exist in the word list. Note that beginWord  | 
 | 18 | + * is not a transformed word.  | 
 | 19 | + * <p>  | 
 | 20 | + * Note:  | 
 | 21 | + * - Return 0 if there is no such transformation sequence.  | 
 | 22 | + * - All words have the same length.  | 
 | 23 | + * - All words contain only lowercase alphabetic characters.  | 
 | 24 | + * - You may assume no duplicates in the word list.  | 
 | 25 | + * - You may assume beginWord and endWord are non-empty and are not the same.  | 
 | 26 | + * <p>  | 
 | 27 | + * Example 1:  | 
 | 28 | + * Input:  | 
 | 29 | + * beginWord = "hit",  | 
 | 30 | + * endWord = "cog",  | 
 | 31 | + * wordList = ["hot","dot","dog","lot","log","cog"]  | 
 | 32 | + * <p>  | 
 | 33 | + * Output: 5  | 
 | 34 | + * <p>  | 
 | 35 | + * Explanation: As one shortest transformation is "hit" -> "hot" -> "dot" -> "dog" -> "cog",  | 
 | 36 | + * return its length 5.  | 
 | 37 | + * <p>  | 
 | 38 | + * Example 2:  | 
 | 39 | + * Input:  | 
 | 40 | + * beginWord = "hit"  | 
 | 41 | + * endWord = "cog"  | 
 | 42 | + * wordList = ["hot","dot","dog","lot","log"]  | 
 | 43 | + * <p>  | 
 | 44 | + * Output: 0  | 
 | 45 | + * <p>  | 
 | 46 | + * Explanation: The endWord "cog" is not in wordList, therefore no possible transformation.  | 
 | 47 | + *  | 
 | 48 | + * @author rampatra  | 
 | 49 | + * @since 2019-08-15  | 
 | 50 | + */  | 
 | 51 | +public class WordLadder {  | 
 | 52 | + | 
 | 53 | +    /**  | 
 | 54 | +     * Runtime: <a href="https://leetcode.com/submissions/detail/251953011/">78 ms</a>.  | 
 | 55 | +     *  | 
 | 56 | +     * @param beginWord  | 
 | 57 | +     * @param endWord  | 
 | 58 | +     * @param wordList  | 
 | 59 | +     * @return  | 
 | 60 | +     */  | 
 | 61 | +    public static int ladderLength(String beginWord, String endWord, List<String> wordList) {  | 
 | 62 | +        int L = beginWord.length();  | 
 | 63 | +        Map<String, Set<String>> originalToTransformedWordMap = new HashMap<>();  | 
 | 64 | +        Queue<Pair<String, Integer>> queue = new LinkedList<>();  | 
 | 65 | + | 
 | 66 | +        wordList.forEach(word -> {  | 
 | 67 | +                    String transformedWord;  | 
 | 68 | +                    for (int i = 0; i < L; i++) {  | 
 | 69 | +                        transformedWord = word.substring(0, i) + "*" + word.substring(i + 1, L);  | 
 | 70 | +                        originalToTransformedWordMap.putIfAbsent(transformedWord, new HashSet<>());  | 
 | 71 | +                        originalToTransformedWordMap.get(transformedWord).add(word);  | 
 | 72 | +                    }  | 
 | 73 | +                }  | 
 | 74 | +        );  | 
 | 75 | + | 
 | 76 | +        Set<String> visited = new HashSet<>();  | 
 | 77 | +        queue.add(new Pair<>(beginWord, 1));  | 
 | 78 | +        visited.add(beginWord);  | 
 | 79 | + | 
 | 80 | +        while (!queue.isEmpty()) {  | 
 | 81 | +            Pair<String, Integer> currPair = queue.poll();  | 
 | 82 | +            String word = currPair.getKey();  | 
 | 83 | +            Integer level = currPair.getValue();  | 
 | 84 | + | 
 | 85 | +            if (word.equals(endWord)) {  | 
 | 86 | +                return level;  | 
 | 87 | +            }  | 
 | 88 | + | 
 | 89 | +            String transformedWord;  | 
 | 90 | +            for (int i = 0; i < L; i++) {  | 
 | 91 | +                transformedWord = word.substring(0, i) + "*" + word.substring(i + 1, L);  | 
 | 92 | + | 
 | 93 | +                for (String originalWord : originalToTransformedWordMap.getOrDefault(transformedWord, Collections.emptySet())) {  | 
 | 94 | +                    if (!visited.contains(originalWord)) {  | 
 | 95 | +                        queue.add(new Pair<>(originalWord, level + 1));  | 
 | 96 | +                        visited.add(originalWord);  | 
 | 97 | +                    }  | 
 | 98 | +                }  | 
 | 99 | +            }  | 
 | 100 | +        }  | 
 | 101 | + | 
 | 102 | +        return 0;  | 
 | 103 | +    }  | 
 | 104 | + | 
 | 105 | +    public static void main(String[] args) {  | 
 | 106 | +        assertEquals(5, ladderLength("hit", "cog", Arrays.asList("hot", "dot", "dog", "lot", "log", "cog")));  | 
 | 107 | +        assertEquals(0, ladderLength("hit", "cog", Arrays.asList("hot", "dot", "dog", "lot", "log")));  | 
 | 108 | +    }  | 
 | 109 | +}  | 
0 commit comments