-
Notifications
You must be signed in to change notification settings - Fork 1
/
StrDist.cpp
40 lines (27 loc) · 1018 Bytes
/
StrDist.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
/*
Given a string and a non-empty substring sub, compute recursively the largest substring which starts and ends with sub and return its length.
strDist("catcowcat", "cat") → 9
strDist("catcowcat", "cow") → 3
strDist("cccatcowcatxx", "cat") → 9
*/
#include <iostream>
using namespace std;
int strDist(string str, string substr){
if(str.empty() || substr.length() > str.length())
return 0;
if(substr.compare(str.substr(0, substr.length())) != 0)
return strDist(str.substr(1), substr);
if(substr.compare(str.substr(str.length() - substr.length(), substr.length())) != 0)
return strDist(str.substr(0, str.length() - 1), substr);
return str.length();
}
int main(){
cout << "Enter String : ";
string str;
cin >> str;
string substr;
cout << "Enter Substring : ";
cin >> substr;
cout << "Length of largest substring which starts and end with " << substr << " in string " << str << " is " << strDist(str, substr) << endl;
return 0;
}