classSolution {publicbooleanisOneEditDistance(String s,String t) {// s for short, t for long;if (s.length() >t.length()) returnisOneEditDistance(t, s);// length diffint lenS =s.length(), lenT =t.length();if (lenT - lenS >1) returnfalse;// walkfor (int i =0; i < lenS; i++) {if (s.charAt(i) !=t.charAt(i)) {returns.substring(i+1).equals(t.substring(i+1))||s.substring(i).equals(t.substring(i+1)); } }return lenT > lenS; // this one is pretty counterintuitive... }}
classSolution {publicbooleanisOneEditDistance(String s,String t) {// edge casesif (s ==null|| t ==null||s.equals(t) ||Math.abs(s.length() -t.length()) >1) returnfalse;// short longString shrt =s.length() >t.length() ? t : s;String lng =s.length() >t.length() ? s : t;for (int i =0; i <shrt.length(); i++) {if (shrt.charAt(i) !=lng.charAt(i)) {returnshrt.substring(i+1).equals(lng.substring(i+1)) // same length||shrt.substring(i).equals(lng.substring(i+1)); // long string has one more char } }returntrue; }}