Max length of substring-palindrome

  • Context: C/C++ 
  • Thread starter Thread starter member 428835
  • Start date Start date
  • Tags Tags
    Length Max
Click For Summary
SUMMARY

The discussion focuses on finding the maximum length of a substring palindrome using a C++ implementation. The proposed algorithm utilizes a for loop to iterate through the string and a while loop to compare characters from both ends towards the center. Key issues identified include an unreachable condition in the code where the check for L equal to R cannot be executed due to the while loop's exit condition. The solution provided is a class named Solution with a method longestPalindrome that returns the maximum palindrome length.

PREREQUISITES
  • C++ programming language knowledge
  • Understanding of string manipulation
  • Familiarity with algorithms for palindrome detection
  • Basic knowledge of control structures (for and while loops)
NEXT STEPS
  • Debug the C++ code to ensure the condition for L equal to R is reachable
  • Research advanced palindrome detection algorithms, such as Manacher's algorithm
  • Explore time complexity analysis for string algorithms
  • Learn about dynamic programming approaches to substring problems
USEFUL FOR

Software developers, particularly those working with string algorithms, computer science students, and anyone interested in optimizing palindrome detection in C++.

member 428835
Hi PF!

I'm trying to find the max length of a sub-string palindrome. My logic is to have a left-most index called start that starts from string loc 0 and traverses to the end of the string via a for loop. Next I was thinking a while loop as long as L < R: I'd have a left index L=start and a right index R = string.length(). If ##L \neq R##, then ##R = R-1## and continue the while loop. However, if ##L = R## then ##L++## and ##R--##, we count this as a match, and continue. Only until L and R are the same (odd palindrome) or 1 away (even palindrome) do we replace the max palindrome length. However, I have a few bugs. Here's what I have, and any help is much appreciated.

C++:
#include <iostream>
#include <vector>
#include <string> // for string class
class Solution {
public:
    int longestPalindrome(std::string str) {
        int n = str.length();
        int maxLen = 1;
        int count = 0;
        for (int start = 0; start < n; start++) {
            int L = start;
            int R = n - 1;
           
            while (R > L) {
                if (str[R] != str[L]) {
                    R--;
                    continue;
                }
                count ++;
                if (L == R) {
                    maxLen = std::max(maxLen, 2*count+1);
                    count = 0;
                    break;
                }
                if (L == R - 1) {
                    maxLen = std::max(maxLen, 2*count);
                    count = 0;
                    break;
                }
                L++;
                R--;
            }
        }
        return maxLen;
    }
};
int main()
{
    std::string s_ = "yyureeeerl";
    Solution ob1;
    auto sol = ob1.longestPalindrome(s_);
    std::cout << sol << "\n";

    return 0;
}
 
Technology news on Phys.org
I didn't spend that much time on your code, but the one obvious problem I can see is that the if(L==R) block is unreachable because the while loop is over when that happens!
 
  • Like
Likes   Reactions: member 428835

Similar threads

  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 22 ·
Replies
22
Views
4K
Replies
55
Views
7K
Replies
5
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 40 ·
2
Replies
40
Views
3K
  • · Replies 36 ·
2
Replies
36
Views
6K
  • · Replies 11 ·
Replies
11
Views
4K
  • · Replies 118 ·
4
Replies
118
Views
10K