-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay3.cpp
More file actions
81 lines (70 loc) · 1.83 KB
/
Day3.cpp
File metadata and controls
81 lines (70 loc) · 1.83 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
77
78
79
80
81
/*
This problem was asked by Amazon.
Given a string, find the longest palindromic contiguous substring. If there are more than one with the maximum length, return any one.
For example, the longest palindromic substring of "aabcdcb" is "bcdcb". The longest palindromic substring of "bananas" is "anana".
*/
#include <bits/stdc++.h>
using namespace std;
bool checkPalindrome(string s, int i, int j)
{
int len = s.length();
int left = i;
int right = j - 1;
while (left < right)
{
if (s[left++] != s[right--])
{
return false;
}
}
return true;
}
// Brute Force Time: O(n^3) Space: O(1)
string longest_palindromic_substring(string s)
{
int len = s.length();
for (int i = len; i > 0; i--)
{
for (int st = 0; st <= len - i; st++)
{
if (checkPalindrome(s, st, st + i))
{
return s.substr(st, i);
}
}
}
return "";
}
// Expand Around Centre Approach, Time: O(n^2), Space: O(1)
string lps(string s)
{
int len = s.size();
auto expand = [&](int left, int right)
{
while (left >= 0 and right < len and s[left] == s[right])
{
left--;
right++;
}
return s.substr(left + 1, right - left - 1);
};
string maxStr = s.substr(0, 1);
for (int i = 0; i < len - 1; i++)
{
string a = expand(i, i);
string b = expand(i, i + 1);
if (a.size() > maxStr.size())
maxStr = a;
if (b.size() > maxStr.size())
maxStr = b;
}
return maxStr;
}
int main()
{
string s1 = "aabcdcb";
string s2 = "bananas";
cout << "Longest palindromic substring of " << s1 << " is: " << lps(s1) << endl;
cout << "Longest palindromic substring of " << s2 << " is: " << lps(s2) << endl;
return 0;
}