-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1.5(oneWay).cpp
More file actions
76 lines (68 loc) · 1.23 KB
/
1.5(oneWay).cpp
File metadata and controls
76 lines (68 loc) · 1.23 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
3 types of edit can be performed on a string. insert, remove, or replace.
Write a function to check if they are one edit away
*/
#include <iostream>
#include <string>
using namespace std;
/*one edit away. delete, add, replace */
bool replace(string first,string second){
bool foundDiff = false;
for(int i=0; i<first.size(); i++){
if(first[i]!=second[i]){
if(foundDiff){
return false;
}else
{
foundDiff =true;
}
}
}
return true;
}
bool deleteaddfunction(string first, string second)
{
int index1 = 0;
int index2 = 0;
while(index1<first.size() && index2<second.size())
{
if(first[index1]!=second[index2])
{
if(index2!=index1)
{
return false;
}
index2++;
}
else
{
index1++;
index2++;
}
}
return true;
}
bool oneEditAway(string first, string second){
if(first.size()==second.size()){
return replace(first, second);
}
else if(first.size()+1==second.size()){
return deleteaddfunction(first, second);
}
else if(first.size()==second.size()+1){
return deleteaddfunction(first, second);
}
return false;
}
int main(){
string str1;
string str2;
cin>>str1;
cin>>str2;
if(oneEditAway(str1, str2)){
cout<<"yes!";
}else{
cout<<"not one edit away";
}
return 0;
}