Question

[Write a Java Program] This question is kind of like this Word Ladder question (similar, but...

[Write a Java Program]

This question is kind of like this Word Ladder question (similar, but not exactly same) at here: https://leetcode.com/problems/word-ladder/description/

and its solution (could be a hint) is at here: https://leetcode.com/problems/word-ladder/discuss/

Write a java program for the following task. Many thanks!

Question is:

0 0
Add a comment Improve this question Transcribed image text
Answer #1
class WordNode{
    String word;
    int numSteps;
    WordNode pre;
 
    public WordNode(String word, int numSteps, WordNode pre){
        this.word = word;
        this.numSteps = numSteps;
        this.pre = pre;
    }
}
 
public class Solution {
    public List<List<String>> findLadders(String start, String end, Set<String> dict) {
        List<List<String>> result = new ArrayList<List<String>>();
 
        LinkedList<WordNode> queue = new LinkedList<WordNode>();
        queue.add(new WordNode(start, 1, null));
 
        dict.add(end);
 
        int minStep = 0;
 
        HashSet<String> visited = new HashSet<String>();  
        HashSet<String> unvisited = new HashSet<String>();  
        unvisited.addAll(dict);
 
        int preNumSteps = 0;
 
        while(!queue.isEmpty()){
            WordNode top = queue.remove();
            String word = top.word;
            int currNumSteps = top.numSteps;
 
            if(word.equals(end)){
                if(minStep == 0){
                    minStep = top.numSteps;
                }
 
                if(top.numSteps == minStep && minStep !=0){
                    //nothing
                    ArrayList<String> t = new ArrayList<String>();
                    t.add(top.word);
                    while(top.pre !=null){
                        t.add(0, top.pre.word);
                        top = top.pre;
                    }
                    result.add(t);
                    continue;
                }
 
            }
 
            if(preNumSteps < currNumSteps){
                unvisited.removeAll(visited);
            }
 
            preNumSteps = currNumSteps;
 
            char[] arr = word.toCharArray();
            for(int i=0; i<arr.length; i++){
                for(char c='a'; c<='z'; c++){
                    char temp = arr[i];
                    if(arr[i]!=c){
                        arr[i]=c;
                    }
 
                    String newWord = new String(arr);
                    if(unvisited.contains(newWord)){
                        queue.add(new WordNode(newWord, top.numSteps+1, top));
                        visited.add(newWord);
                    }
 
                    arr[i]=temp;
                }
            }
 
 
        }
 
        return result;
    }
}
Add a comment
Know the answer?
Add Answer to:
[Write a Java Program] This question is kind of like this Word Ladder question (similar, but...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 1) IN JAVA Write a short program that tests if an inputted word is a palindrome....

    1) IN JAVA Write a short program that tests if an inputted word is a palindrome. A palindrome is a word that is spelled exactly the same forward and backwards such as radar or mom. You must look at individual characters for the solution, do not use a premade method such as reverse! ALSO DO THIS IN ONE SINGULAR MAIN WITH NO STATIC METHODS

  • For this lab you will write a Java program that plays a simple Guess The Word...

    For this lab you will write a Java program that plays a simple Guess The Word game. The program will prompt the user to enter the name of a file containing a list of words. These words mustbe stored in an ArrayList, and the program will not know how many words are in the file before it starts putting them in the list. When all of the words have been read from the file, the program randomly chooses one word...

  • Write a Java program that reads a word and prints its bigrams substrings. A character bigram...

    Write a Java program that reads a word and prints its bigrams substrings. A character bigram is defined as a continuous sequence of two characters in a word. For example, if the user provides the input "rum", the program prints ru um Hint: 1. set two pointers, one always moves from the beginning of the string and the other moves from i+2, in which i is the current position of the first pointer. 2. print an error message if the...

  • IN JAVA ONLY - Please do not copy answers from similar question; those solutions DON'T work. 1. W...

    IN JAVA ONLY - Please do not copy answers from similar question; those solutions DON'T work. 1. Write a hangman’s program where the user is a computer that has to guess the word “JAVA.” The computer uses a BRUTE FORCE method. a. First write the program that shows (prints) each guess (the letter) and also draws the result (line by line, circle by circle) when the guess is wrong. b. What does the brute force method do exactly? c. The...

  • Write the following program in Java: You are to write a Java program that reads an...

    Write the following program in Java: You are to write a Java program that reads an input file containing a list of words and their synonyms. You will repeatedly ask the user if they want to look up the synonyms of a word, add a new synonym for a word, or quit. Here is a sample run of the program (note that the program is expecting the input file, thesaurus.txt to be in the working directory for the code.): Enter...

  • JAVA QUESTION 2.One use of a Stack is to reverse the order of input. Write a...

    JAVA QUESTION 2.One use of a Stack is to reverse the order of input. Write a complete method that reads a series of Strings from the user. The user enters "end" to stop inputting words. Then, output the Strings in reverse order of how they were entered. Do not output the String “end”. Use a stack to accomplish this task. Invoke only the methods push, pop, peek, and isEmpty on the stack object. Here is an example of how the...

  • Programming Assignment Objective Write a Java program that utilizes multiple classes. Write a Java program that...

    Programming Assignment Objective Write a Java program that utilizes multiple classes. Write a Java program that utilizes inheritance in a practical manner. Problem: Quiz Bowl Your high school quiz bowl team has been losing its edge and needs to find a method to improve. Knowing that you are a savvy programmer, your coach asks you to write a program that the team members can use to hone their skills. Quiz Bowl questions come in three varieties: True/False Multiple Choice (variable...

  • Today you are to write a Java program that will prompt for and read 2 words...

    Today you are to write a Java program that will prompt for and read 2 words of equal length entered by the user, and create a new word which contains the last letter of the 1st word, the last letter of the 2nd word, followed by the second to last character of the 1st word, followed by the second to last character of the 2nd word and so on. Be sure to use the same format and wording as in...

  • Java Question: Write a multithread Java program that performs matrix multiplication. In the program, you need...

    Java Question: Write a multithread Java program that performs matrix multiplication. In the program, you need to calculate each element in the result matrix in a separate thread. The code should be generic with regard to matrix size. The sizes of three matrices should be stored in variable and used. The two operand matrices and the result matrix should be printed. Thanks

  • Could you guys write an efficient java program to implement BST. Your java program should read...

    Could you guys write an efficient java program to implement BST. Your java program should read words and its corresponding French and store it in a BST. Then read every English word and print its corresponding French by searching in BST. Assume your java program is in xxxxx5.java file (5th java project), where xxxxx is the first 5 characters of your last name. To compile: javac xxxxx5.java To execute: java xxxxx5 < any data file name Your main method should...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT