У меня все тесты проходит, ему что вообще не нравится?
package com.javarush.task.task39.task3909;
/*
Одно изменение
*/
public class Solution {
public static void main(String[] args) {
System.out.println(isOneEditAway("am", "mak"));
System.out.println(isOneEditAway("aline", "line"));
System.out.println(isOneEditAway("line", "liene"));
System.out.println(isOneEditAway("line", "linse"));
System.out.println(isOneEditAway("line", "linsess"));
System.out.println(isOneEditAway("01", "102"));
System.out.println(isOneEditAway("1032", "102"));
System.out.println(isOneEditAway("123", "1023"));
}
public static boolean isOneEditAway(String first, String second) {
int length = first.length() - second.length();
String longest;
String shortest;
if (first == null || second == null || Math.abs(length) > 1)
return false;
if (first.equals(second)) return true;
int mistakesAllowed = 1;
if (first.length() == second.length()) {
for (int i = 0; i < first.length(); i++) {
if (first.charAt(i) != second.charAt(i)) {
mistakesAllowed--;
if (mistakesAllowed < 0) {
return false;
}
}
}
} else {
if (length == 1) {
longest = first;
shortest = second;
} else {
longest = second;
shortest = first;
}
if (longest.contains(shortest))
return true;
for (int i = 0; i < longest.length(); i++) {
if (shortest.charAt(i) != longest.charAt(i)) {
mistakesAllowed--;
longest = longest.substring(i + 1);
i--;
if (mistakesAllowed < 0) {
return false;
}
}
}
}
return true;
}
}