forked from gutty333/Medium-Programming-Challenges
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path38_PalindromicSubstring.cpp
83 lines (67 loc) · 1.96 KB
/
38_PalindromicSubstring.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// For this challenge you will be finding the longest palindromic substring.
/*
have the function PalindromicSubstring(str) take the str parameter being passed and find the longest palindromic substring, which means the longest substring which is read the same forwards as it is backwards. For example: if str is "abracecars" then your program should return the string racecar because it is the longest palindrome within the input string.
The input will only contain lowercase alphabetic characters. The longest palindromic substring will always be unique, but if there is none that is longer than 2 characters, return the string none.
*/
#include <iostream>
#include <string>
using namespace std;
/*
Loop through the string
analyze sub strings with more than 3 characters
continue to analyze from that index
keep track of the longest palindrome and repeat the steps
*/
// Function checking for palindrome
bool palindrome(string value)
{
int index = value.length() - 1;
for (int x = 0; x < value.length() / 2; x++)
{
if (value[x] != value[index])
{
return false;
}
index--;
}
return true;
}
string PalindromicSubstring(string str)
{
string temp;
string result;
for (int x = 0; x < str.length(); x++)
{
temp = str[x];
// Loop to check for a substring palindrome
for (int y = x + 1; y < str.length(); y++)
{
temp += str[y];
// Only analyze when we have at least 3 characters and the substring is a palindrome
if (temp.length() >= 3 && palindrome(temp))
{
// Keep track of the longest palindrome substring
if (temp.length() > result.length())
{
result = temp;
}
}
}
}
if (result.length() == 0)
{
return "none";
}
else
{
return result;
}
}
int main()
{
cout << PalindromicSubstring("abracecars") << endl; // racecar
cout << PalindromicSubstring("hellosannasmith") << endl; // sannas
cout << PalindromicSubstring("abcdefgg") << endl; // none
cout << PalindromicSubstring("acaaca") << endl; // acaaca
return 0;
}