D
D
devalone2017-11-05 21:23:52
Programming
devalone, 2017-11-05 21:23:52

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"

Output:
"wow"

Input data:
"123wow123"

Output:
"wow"

Input data:
"abcdebob"

Output:
"bob"

Input data:
"rokor aba"

Output:
"rokor"

My solution that fails one hidden test:
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 question

Ask a Question

731 491 924 answers to any question