Everything I tested comes out as expected. I also have no idea why it works for different lengths, but not for the same lengths, even when using the same method (slightly adjusted ofc so the counters i en j increase for same length). Won't validate when checked seperately either. I even added the checks for ASCII and to see if the string is empty, yet even the empty check won't validate (even when I added the first == null to the checks too). That's not exactly a rocketScience coding anymore at this point. What is going on here???
package com.codegym.task.task39.task3909;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
/*
One change
*/
public class Solution {
public static void main(String[] args) {
String str1 = "String method tutorial";
String str2 = "String methods tutorial";
String str3 = "String method tutorials";
String str4 = "Strong method tutorial";
String str2a = "Strong methods tutorial";
String str3a = "String mithod tutorials";
System.out.println(isOneEditAway(str1, str2)); // true step 3
System.out.println(isOneEditAway(str3, str1)); // true step 2
System.out.println(isOneEditAway(str1, str4)); // true step 1
System.out.println(isOneEditAway(str1, str2a)); // false step 3
System.out.println(isOneEditAway(str3a, str1)); // false step 2
System.out.println(isOneEditAway(str2, str2)); // false step 1
}
public static boolean isOneEditAway(String first, String second) {
if (first.length() == 0) { //check if string is empty
if (second.length() == 0){ //check if string2 is empty
return false;
}
return second.length() == 1; //check if string2 is only 1 character.
}
if (second.length() == 0) { //check if string is empty
return first.length() == 1; //check if string1 is only 1 character.
}
if (!isPureAscii(first) || !isPureAscii(second)){ // check if the string is only ASCII
return false;
}
if (Math.abs(first.length() - second.length()) > 1){ // make sure the length has no more than 1 difference;
return false;
}
if (first.length() != second.length()){
return getDifferencesDifLength(first, second); // different string lengths
}
if (first.matches(second)){ // checks if the entire string matches string2
return false;
}
return getDifferencesSameLength(first, second);// same lengths (should be able to be combined, but won't validate.
}
public static boolean getDifferencesSameLength(String a, String b){
String first = a.toLowerCase();
String second = b.toLowerCase();
int count = 0;
for (int i = 0; i < a.length();i++){
if (first.charAt(i) != second.charAt(i)) {
count++;
if (count == 2){
break;
}
}
}
return count == 1;
}
public static boolean getDifferencesDifLength(String a, String b) {
String first = a.toLowerCase();
String second = b.toLowerCase();
int len1 = first.length();
int len2 = second.length();
int count = 0;
int i = 0, j = 0;
while (i < len1 && j < len2) {
if (first.charAt(i) != second.charAt(j)) {
if (count == 1) {
return false;
}
if (len1 > len2){
i++;
}
else if (len1 < len2){
j++;
}
count++;
} else {
i++;
j++;
}
}
if (i < len1 || j < len2) count++;
return count == 1;
}
public static boolean isPureAscii(String v) {
return StandardCharsets.US_ASCII.newEncoder().canEncode(v);
}
}