HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Change one word into another by picking words that differ by one character

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
pickingintowordscharacteronewordthatanotherdifferchange

Problem

There is a file with two words with the same length. Call them first and last.

Also there is a file - dictionary with a lot of different words.

The task is to find shortest chain of words from dictionary from first word to last so that two neighboring words different just one character. All words of chain must be the same length.

For example:

Input:

// first and last words

top
son


// dictionary

top
son
pop
pot
dot
ton
son
soft
task


Output:

top
ton
son


My program works, but I am not sure that it is good code. So I ask you to view my code.

public class Main {

    // args[0] - is path to file with first and last words
    // args[1] - is path to file with dictionary
    public static void main(String[] args) {
        try {
            List firstLastWords = FileParser.getWords(args[0]);
            // System.out.println(firstLastWords);
            int sizeOfWords = firstLastWords.get(1).length();

            List dictionary = WordHandler.getSameLengthWords(
                    FileParser.getWords(args[1]), sizeOfWords);
            // System.out.println(dictionary);

            Tree tree = new Tree(dictionary, firstLastWords);
            tree.print();
            tree.findShortestSuitableChain();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}


```
public class FileParser {
public FileParser() {
}

final static Charset ENCODING = StandardCharsets.UTF_8;

public static List getWords(String filePath) throws IOException {
List list = new ArrayList();
Path path = Paths.get(filePath);

try (BufferedReader reader = Files.newBufferedReader(path, ENCODING)) {
String line = null;
while ((line = reader.readLine()) != null) {
String line1 = line.trim().replaceAll("\uFEFF", "");
list.add(line1);
}
reader.close();

for (int i = 0; i < list.size(); i++) {

Solution

WordHandler

public static boolean isOneLetterDifference(String baseWord,String checkWord) {
    char[] baseChars = baseWord.toCharArray();
    char[] checkChars = checkWord.toCharArray();
    int diffLetters = 0;

    if (baseChars.length != checkChars.length) {
        return false;
    }

    for (int i = 0; i < baseChars.length; i++) {
        if (baseChars[i] != checkChars[i]) {
            diffLetters++;
        }
    }

    if (diffLetters == 1) {
        return true;
    } else {
        return false;
    }
}


There's a few improvements here.

First,

if (diffLetters == 1) {
    return true;
} else {
    return false;
}


"If condition is true, return true, else if condition is not true AKA false, return false." Such statements can be replaced with "return condition".

return diffLetters == 1;


Second,

char[] baseChars = baseWord.toCharArray();
    char[] checkChars = checkWord.toCharArray();
    int diffLetters = 0;

    if (baseChars.length != checkChars.length) {
        return false;
    }


This guard clause can be done sooner. There's no need to convert to char array before checking the length.

if(baseWord.length != checkWord.length){
    return false;
}


Third,

for (int i = 0; i < baseChars.length; i++) {
        if (baseChars[i] != checkChars[i]) {
            diffLetters++;
        }
    }


you can exit early once you know more than 1 letter differs.

for (int i = 0; i  1){
                return false;
            }
        }
    }

Code Snippets

public static boolean isOneLetterDifference(String baseWord,String checkWord) {
    char[] baseChars = baseWord.toCharArray();
    char[] checkChars = checkWord.toCharArray();
    int diffLetters = 0;

    if (baseChars.length != checkChars.length) {
        return false;
    }

    for (int i = 0; i < baseChars.length; i++) {
        if (baseChars[i] != checkChars[i]) {
            diffLetters++;
        }
    }

    if (diffLetters == 1) {
        return true;
    } else {
        return false;
    }
}
if (diffLetters == 1) {
    return true;
} else {
    return false;
}
return diffLetters == 1;
char[] baseChars = baseWord.toCharArray();
    char[] checkChars = checkWord.toCharArray();
    int diffLetters = 0;

    if (baseChars.length != checkChars.length) {
        return false;
    }
if(baseWord.length != checkWord.length){
    return false;
}

Context

StackExchange Code Review Q#88334, answer score: 5

Revisions (0)

No revisions yet.