[LeetCode] 21 word Jielong I

subject

Given two words (beginWord and endWord) and a dictionary, find the length of the shortest conversion sequence from beginWord to endWord. The conversion should follow the following rules:

1. Only one letter can be changed at a time. 2. The middle word in the conversion process must be the word in the dictionary.

explain:

  • If such a conversion sequence does not exist, 0 is returned.
  • All words have the same length.
  • All words are made up of lowercase letters only.
  • There are no duplicate words in the dictionary.
  • You can assume that beginWord and endWord are not empty, and they are different.

Example 1:

Input:
beginWord = "hit",
endWord = "cog",
wordList = ["hot","dot","dog","lot","log","cog"]

Output: 5

Explanation: the shortest conversion sequence is "hit" - > "hot" - > "dot" - > "dog" - > "cog",
     Returns its length of 5.

Example 2:

Input:
beginWord = "hit"
endWord = "cog"
wordList = ["hot","dot","dog","lot","log"]

Output: 0

Explanation: endWord "cog" is not in the dictionary, so it cannot be converted.

thinking

https://leetcode-cn.com/problems/word-ladder/solution/dan-ci-jie-long-by-leetcode/

code

package com.janeroad;

import javafx.util.Pair;
import java.util.*;

public class test9 {

public int ladderLength(String beginWord, String endWord, List<String> wordList) {
    // Because all words are the same length.
    int L = beginWord.length();
    // A dictionary that holds word combinations that can be composed of any given word. By changing one letter at a time
    Map<String, List<String>> allComboDict = new HashMap<>();

    wordList.forEach(
            word -> {
                for (int i = 0; i < L; i++) {
                    // Key is the generic word
                    // Value is a list of words which have the same intermediate generic word.
                    String newWord = word.substring(0, i) + '*' + word.substring(i + 1, L);
                    List<String> transformations = allComboDict.getOrDefault(newWord, new ArrayList<>());
                    transformations.add(word);
                    allComboDict.put(newWord, transformations);
                }
            });

    // BFS queue
    Queue<Pair<String, Integer>> Q = new LinkedList<>();
    Q.add(new Pair(beginWord, 1));

    // Visit to make sure we don't repeat the same words.
    Map<String, Boolean> visited = new HashMap<>();
    visited.put(beginWord, true);

    while (!Q.isEmpty()) {
        Pair<String, Integer> node = Q.remove();
        String word = node.getKey();
        int level = node.getValue();
        for (int i = 0; i < L; i++) {

            // Middle word of current word
            String newWord = word.substring(0, i) + '*' + word.substring(i + 1, L);

            // The next state is to share all words in the same intermediate state.
            for (String adjacentWord : allComboDict.getOrDefault(newWord, new ArrayList<>())) {
                // If at any time we find what we're looking for
                // The closing word - we can return the answer.
                if (adjacentWord.equals(endWord)) {
                    return level + 1;
                }
                // Otherwise, it is added to the BFS queue. Also marked as accessed
                if (!visited.containsKey(adjacentWord)) {
                    visited.put(adjacentWord, true);
                    Q.add(new Pair(adjacentWord, level + 1));
                }
            }
        }
    }

    return 0;
}




public static void main(String[] args) {
    List<String> stringList = new LinkedList<String>(){{
        add("hot");
        add("dot");
        add("dog");
        add("lot");
        add("cog");
    }};
    test9 test9=new test9();
    System.out.println(test9.ladderLength("hit","cog",stringList));
}
}

Tags: Programming Java

Posted on Tue, 26 May 2020 11:59:32 -0400 by flit