*JAVA* *Code needed: I posted thjs previously and got a detailed description but
ID: 3737584 • Letter: #
Question
*JAVA*
*Code needed: I posted thjs previously and got a detailed description but no code and was told to repost for the code.*
A word can be changed to another word by a one-character substitution. Assume that a dictionary of five-letter words exists. Give an algorithm to determine whether a word A can be transformed to a word B by a series of one-character substitutions, and if so, outputs the corresponding sequence of words. For example, bleed converts to blood by the sequence bleed, blend, blond, blood.
Explanation / Answer
Below is the running code for the above question:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package cheggquestions;
import java.util.*;
/**
*
* @author Prashant Tomer
*/
public class WordConversion {
public static void main(String aa[])
{
Scanner sc=new Scanner(System.in);
ArrayList <String>dictionary=new ArrayList(); //ArrayList for adding disctionary elements
dictionary.add("mango");
dictionary.add("grape");
dictionary.add("blood");
dictionary.add("torke");
String word=sc.next();
for(int i=0;i<dictionary.size();i++)
{
String word1="";
if(word.substring(0,1).equalsIgnoreCase(dictionary.get(i).substring(0, 1)))
{
for(int j=2;j<5;j++)
{
if(word.charAt(j)!=dictionary.get(i).charAt(j)) //check the character equality in input string and dictionary
{
System.out.println("word at::"+word.charAt(j)+" dictinary at::"+dictionary.get(i).charAt(j));
//System.out.println("dictinary at::"+dictionary.get(i).charAt(j));
word=word.replace(word.charAt(j),dictionary.get(i).charAt(j));
//System.out.println("Inside if::"+word);
}
System.out.println("new word::"+word);
}
}
//System.out.println(dictionary.get(i));
}
}
}
O/P:
Input String:groec
word at::o dictinary at::a
new word::graec
word at::e dictinary at::p
new word::grapc
word at::c dictinary at::e
new word::grape
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.