miro
BAN USERThis is a modified version of Garukun's answer. Only with few fixes
public static boolean onlyOneEdit(String first, String second)
{
if(first.equals(second))
{
return false;
}
int l1 = first.length();
int l2 = second.length();
if(l1 - l2 > 1 || l2 - l1 > 1)
{
// At least two edits .. no need to continue
return false;
}
String longer = (l1 > l2)? first : second;
String shorter = (l1 > l2)? second : first;
for(int i = 0; i < shorter.length(); i++)
{
if(longer.charAt(i) != shorter.charAt(i))
{
int shift = (l1 == l2)? 0 : 1;
return longer.substring(i + 1 + shift).equals(shorter.substring(i + 1));
}
}
// No difference detected until the end of the shorter string
return true;
}
- miro November 09, 2014