|  | 
|  | 1 | +public class StringOperationsCount { | 
|  | 2 | + public static void main(String[] args) { | 
|  | 3 | + // NOTE: The following input values will be used for testing your solution. | 
|  | 4 | + // Should return true if inserting a single char, deleting it or replacing it. | 
|  | 5 | + System.out.println(isOneAway("abcde", "abcd")); // should return true | 
|  | 6 | + System.out.println(isOneAway("abde", "abcde")); // should return true | 
|  | 7 | + System.out.println(isOneAway("a", "a")); // should return | 
|  | 8 | + System.out.println(isOneAway("a", "b")); // should return true | 
|  | 9 | + System.out.println(isOneAway("abcdef", "abqdef")); // should return true | 
|  | 10 | + System.out.println(isOneAway("abcdef", "abccef")); // should return true | 
|  | 11 | + System.out.println(isOneAway("abcdef", "abcde")); // should return true | 
|  | 12 | + System.out.println(isOneAway("aaa", "abc")); // should return false beacuse its two character replace | 
|  | 13 | + System.out.println(isOneAway("abcde", "abc")); // should return false | 
|  | 14 | + System.out.println(isOneAway("abc", "abcde")); // should return false | 
|  | 15 | + System.out.println(isOneAway("abc", "bcc")); // should return false | 
|  | 16 | + } | 
|  | 17 | + | 
|  | 18 | + // Implement your solution below. | 
|  | 19 | + public static Boolean isOneAway(String s1, String s2) { | 
|  | 20 | + boolean bool = false; | 
|  | 21 | + int s1Length = s1.length(); | 
|  | 22 | + int s2Length = s2.length(); | 
|  | 23 | + int editDistance = getEditDistance(s1, s2,s1Length,s2Length); | 
|  | 24 | + if(editDistance>1) | 
|  | 25 | + return false; | 
|  | 26 | + else | 
|  | 27 | + return true; | 
|  | 28 | + } | 
|  | 29 | + | 
|  | 30 | + private static int getEditDistance(String s1, String s2,int s1Length, int s2Length) { | 
|  | 31 | + | 
|  | 32 | + if(s1Length==0){ | 
|  | 33 | + return s2Length; | 
|  | 34 | + } | 
|  | 35 | + if(s2Length==0){ | 
|  | 36 | + return s1Length; | 
|  | 37 | + } | 
|  | 38 | + if(s1.charAt(s1Length-1)== s2.charAt(s2Length-1)) | 
|  | 39 | + return getEditDistance(s1,s2,s1Length-1,s2Length-1); | 
|  | 40 | + | 
|  | 41 | + return 1+ min(getEditDistance(s1,s2,s1Length,s2Length-1) | 
|  | 42 | + ,getEditDistance(s1,s2,s1Length-1,s2Length), | 
|  | 43 | + getEditDistance(s1,s2,s1Length-1,s2Length-1)); | 
|  | 44 | + } | 
|  | 45 | + | 
|  | 46 | + private static int min(int x, int y, int z) { | 
|  | 47 | + if (x <= y && x <= z) | 
|  | 48 | + return x; | 
|  | 49 | + if (y <= x && y <= z) | 
|  | 50 | + return y; | 
|  | 51 | + else | 
|  | 52 | + return z; | 
|  | 53 | + } | 
|  | 54 | +} | 
0 commit comments