forked from CodeToExpress/dailycodebase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
oneEditDistance.cpp
57 lines (42 loc) · 1.29 KB
/
oneEditDistance.cpp
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
54
55
56
57
/*
* @author : imkaka
* @date : 28/12/2018
*/
#include<iostream>
#include<string>
using namespace std;
string isOneEditDistanceAway(string str1, string str2){
int mi = str1.size() <= str2.size() ? str1.size() : str2.size();
int ma = str1.size() > str2.size() ? str1.size() : str2.size();
if( (ma - mi) > 1){
return " No! ";
}
string sho = str1.size() < str2.size() ? str1 : str2;
string lon = str1.size() > str2.size() ? str1 : str2;
bool diff = false;
int id1 = 0;
int id2 = 0;
while(id2 < lon.size() && id1 < sho.size()){
if(sho[id1] != lon[id2]) {
if(diff) return "No!";
diff = true;
if(sho.size() == lon.size()){
id1++;
}
}else{
id1++;
}
id2++;
}
return "Yes!";
}
int main(){
string str1 , str2;
cout << "Enter two strings with space in between: ";
cin >> str1 >> str2;
cout << str1 << " " << str2 << " => " <<isOneEditDistanceAway(str1, str2) << endl;
cout << "======= Hard Coded Strings ======= " << endl;
cout << "pale" << " pal" << " " << " => " <<isOneEditDistanceAway("pale", "pal") << endl;
cout << "bake" << " cake" << " " << " => " <<isOneEditDistanceAway("bake", "cake") << endl;
return 0;
}