-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunique.java
53 lines (45 loc) · 1.31 KB
/
unique.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import java.util.*;
public class unique {
static boolean oneEditAway(String s1, String s2){
if(s1.length()==s2.length())
return oneEditReplace(s1,s2);
else if(s1.length()-1==s2.length()) //insertion
return oneInsertion(s2,s1);
else if(s1.length()+1==s2.length())
return oneInsertion(s1,s2); //Deletion
else return false;
}
static boolean oneEditReplace(String s1,String s2){
boolean isDiffer=false;
for(int i=0;i<s1.length();i++){
if(s1.charAt(i)!=s2.charAt(i)){
if(isDiffer){
return false;
}
isDiffer=true;
}
}
return true;
}
static boolean oneInsertion(String s1, String s2){
int idx1=0,idx2=0;
while(idx2<s2.length() && idx1<s1.length()){
if(s1.charAt(idx1) != s2.charAt(idx2)){
if(idx1!=idx2){
return false;
}
else idx2++;
}
else{
idx1++;
idx2++;
}
}
return true;
}
public static void main(String ar[]) {
// unique obj=new unique();
String s1="bake",s2="pale";
System.out.print(oneEditAway(s1,s2));
}
}