Can someone explain how I would go about checking that the new valid word entered is only one character different from previous word???

6 Years
Discussion Span
Last Post by stultuske

Just start writing a simple method eg
public static boolean isValidChange(String word1, String word2) { ...
You have 3 cases to test for - change, add, remove a letter. Code and test those 1 at a time. Start with 1 letter changed, that's probably the easiest.

How would you be able to check to see if only one letter was changed?

I am able to check if a letter was removed/added using .length(), but I can't seem to figure out a method to check if it has been changed..... Thank you in advance



set the value of the second string,
set a difCounter to 0
iterate over the original one by charAt, and compare the char's. if the char's are different, add 1 to your counter.
then replace your second string by the concatenation of the two substrings (part of that string before the different char, part of that string after that different char)
and continue searching for differences, starting from the same position

if your counter turns out to be > 1, you've got more than one character changed.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.