|
| 1 | +#include<bits/stdc++.h> |
| 2 | +using namespace std; |
| 3 | +//three type of modification can perform on string |
| 4 | +// 1. insert -->>other string have one greter length |
| 5 | +// 2. delete -->other string less than one length |
| 6 | +// 3. replace (modify) -->both string same length than replace |
| 7 | + |
| 8 | + |
| 9 | +//bool oneReplace(string source,string check){ |
| 10 | +// bool differanceCheck=false; |
| 11 | +// for(int i=0;i<source.length();i++){ |
| 12 | +// if(source[i]!=check[i]){ |
| 13 | +// if(differanceCheck){ |
| 14 | +// return false; |
| 15 | +// } |
| 16 | +// differanceCheck=true; |
| 17 | +// } |
| 18 | +// } |
| 19 | +// return true; |
| 20 | +//} |
| 21 | +//bool oneInsert(string source,string check){ |
| 22 | +// int index=0,index2=0; |
| 23 | +// |
| 24 | +// string s1=source.length()<check.length()?source:check; |
| 25 | +// string s2=source.length()<check.length()?check:source; |
| 26 | +// while(index<s1.length() && index2<s2.length()){ |
| 27 | +// if(s1[index]!=s2[index2]){ |
| 28 | +// if(index!=index2){ |
| 29 | +// return false; |
| 30 | +// } |
| 31 | +// index2++; |
| 32 | +// |
| 33 | +// }else{ |
| 34 | +// index++; |
| 35 | +// index2++; |
| 36 | +// |
| 37 | +// } |
| 38 | +// } |
| 39 | +// return true; |
| 40 | +//} |
| 41 | +//bool oneaway(string source,string check){ |
| 42 | +// if(source.length()==check.length()){ |
| 43 | +// return oneReplace(source,check); |
| 44 | +// }else if(source.length()-1==check.length()){ //true than it can be one away insert |
| 45 | +// return oneInsert(source,check); |
| 46 | +// }else if(source.length()+1==check.length()){ //true than it can be one away modify(delete) |
| 47 | +// return oneInsert(source,check); |
| 48 | +// } |
| 49 | +// |
| 50 | +//return false; |
| 51 | +//} |
| 52 | +bool oneaway(string source,string check){ |
| 53 | + int s1Len=source.length(),s2Len=check.length(); |
| 54 | + if(abs(s1Len-s2Len)>1){ |
| 55 | + return false; |
| 56 | + } |
| 57 | + string s1=s1Len<s2Len?source:check; |
| 58 | + string s2=s1Len<s2Len?check:source; |
| 59 | + s1Len=s1.length(); |
| 60 | + s2Len=s2.length(); |
| 61 | + int index=0,index2=0; |
| 62 | + bool differance=false; |
| 63 | + bool equalLength=false; |
| 64 | + if(s1Len==s2Len){ |
| 65 | + equalLength=true; |
| 66 | + } |
| 67 | + while(index<s1Len && index2<s2Len){ |
| 68 | + if(s1[index]!=s2[index2]){ |
| 69 | + |
| 70 | + if(differance) return false; |
| 71 | + differance=true; |
| 72 | + |
| 73 | + if(equalLength) index++; |
| 74 | + }else{ |
| 75 | + index++; |
| 76 | + } |
| 77 | + index2++; |
| 78 | + } |
| 79 | + return true; |
| 80 | +} |
| 81 | +int main(){ |
| 82 | + |
| 83 | + string s1="pale"; //source |
| 84 | + string s2="pales";//source |
| 85 | + |
| 86 | + string c1="ple"; //check |
| 87 | + string c2="pale"; //check |
| 88 | + string c3="bale"; //check |
| 89 | + string c4="bae"; //check |
| 90 | + string c5="ale"; //check |
| 91 | + bool flag; |
| 92 | + flag=oneaway(s1,c5); |
| 93 | + |
| 94 | + if(flag){ |
| 95 | + cout<<"True"<<endl; |
| 96 | + }else{ |
| 97 | + cout<<"False"<<endl; |
| 98 | + } |
| 99 | + return 0; |
| 100 | +} |
0 commit comments