Answer the question
In order to leave comments, you need to log in
What is wrong in the elementary code for finding the maximum palindrome?
Task: find the maximum palindrome substring in a string. Those.
Input data:
"wow"
"wow"
"123wow123"
"wow"
"abcdebob"
"bob"
"rokor aba"
"rokor"
bool isSubstringPalindrom(const std::string& str, size_t pos, size_t length)
{
for (size_t i = 0; i < length / 2; ++i) {
if (str[pos + i] != str[pos + length - i - 1])
return false;
}
return true;
}
std::string getStringWithQuotes(const std::string& str)
{
return std::string("\"") + str + "\"";
}
std::string longestPalindromeSubstr(string s) {
if (s.size() < 3)
return getStringWithQuotes("");
// remove FUCKING quotes
s = s.substr(1, s.size() - 2);
for (size_t substrLength = s.size(); substrLength > 1; --substrLength) {
size_t substringsCount = s.size() - substrLength + 1;
for (size_t i = 0; i < substringsCount; ++i) {
if (isSubstringPalindrom(s, i, substrLength))
return getStringWithQuotes(s.substr(i, substrLength));
std::cout << s.substr(i, substrLength) << std::endl;
}
}
return getStringWithQuotes(s.substr(0, 1));
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question