Given two strings, find if they differ by exactly two letters.
Interview Answers
Anonymous
2 Sept 2018
What you can do is to have a sliding window where you are checking the different characters inside :-)
Anonymous
24 Feb 2019
public static boolean twoLetterDiference(String a, String b){
if(a.length() != b.length())
return false;
HashMap mapA = new HashMap();
HashMap mapB = new HashMap();
for(int i = 0; i mB)
dif += mA-mB;
}
return dif == 2;
}
Anonymous
24 Feb 2019
public static boolean twoLetterDiference(String a, String b){
if(a.length() != b.length())
return false;
HashMap mapA = new HashMap();
HashMap mapB = new HashMap();
for(int i = 0; i mB)
dif += mA-mB;
}
return dif == 2;
}
Anonymous
4 Dec 2018
For the sliding window, that works when the characters are "replaced".
Example:
"abcde"
"abcxx"
However, I believe the question must be specified more specifically. If removal of characters is legal, and the removal of one character is still considered "differing" by one character, then the sliding window solution gets more complicated:
Example:
"abcde"
"cde"