Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

C++ Exercises: Length of the longest palindrome in a given string

C++ String: Exercise-20 with Solution

Write a C++ program to find the length of the longest palindrome in a given string (uppercase or lowercase letters).

From Wikipedia,
Given a string s which consists of lowercase or uppercase letters, return the length of the longest palindrome that can be built with those letters.
Note: Uppercase and lowercase letters are treated as distinct (case-sensitive)
A palindrome is a word, number, phrase, or other sequence of characters which reads the same backward as forward, such as madam, racecar.
In computer science, the longest palindromic substring or longest symmetric factor problem is the problem of finding a maximum-length contiguous substring of a given string that is also a palindrome. For example, the longest palindromic substring of "bananas" is "anana". The longest palindromic substring is not guaranteed to be unique; for example, in the string "abracadabra", there is no palindromic substring with length greater than three, but there are two palindromic substrings with length three, namely, "aca" and "ada". In some applications it may be necessary to return all maximal palindromic substrings (that is, all substrings that are themselves palindromes and cannot be extended to larger palindromic substrings) rather than returning only one substring or returning the maximum length of a palindromic substring.

Example-1:

Input: adcdcdy

Output: 5

Example-2:

Input: aaa

Output: 3

Example-3:

Input: aa

Output: 2

Example-4:

Input: abddddeeff

Output: 9

Example-5:

Input: abddddeeff

Output: 1

Sample Solution:

C++ Code:

#include <iostream>

#include<algorithm>
#include <unordered_map>

using namespace std;
 
int longest_Palindrome_length(string str1) {
        unordered_map< char,int >map_data;
        int length = 0, flag = 1;
        for(char x: str1)map_data[x]++;
        for(auto i : map_data){
            if(i.second % 2 == 0){
                length += i.second;
            }
            else{
                length += i.second-1;
                flag = 0;
            }
        }
        if(flag == 0){
            length = length + 1;
        }
        return length;
    }

int main()
{
  char str1[] = "adcdcdy";
  cout << "Original string: " << str1;
  cout << "\nLength of the longest palindrome of the said string: " << longest_Palindrome_length(str1);
  char str2[] = "aaa";
  cout << "\n\nOriginal string: " << str2;
  cout << "\nLength of the longest palindrome of the said string: " << longest_Palindrome_length(str2);
  char str3[] = "aa";
  cout << "\n\nOriginal string: " << str3;
  cout << "\nLength of the longest palindrome of the said string: " << longest_Palindrome_length(str3);
  char str4[] = "abddddeeff";
  cout << "\n\nOriginal string: " << str4;
  cout << "\nLength of the longest palindrome of the said string: " << longest_Palindrome_length(str4);
  char str5[] = "PYTHON";
  cout << "\n\nOriginal string: " << str5;
  cout << "\nLength of the longest palindrome of the said string: " << longest_Palindrome_length(str5);
  return 0;    
}

Sample Output:

Original string: adcdcdy
Length of the longest palindrome of the said string: 5

Original string: aaa
Length of the longest palindrome of the said string: 3

Original string: aa
Length of the longest palindrome of the said string: 2

Original string: abddddeeff
Length of the longest palindrome of the said string: 9

Original string: PYTHON
Length of the longest palindrome of the said string: 1

Flowchart:

Flowchart: Reverse only the vowels of a given string.

C++ Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a C++ program to reverse only the vowels of a given string.

Next: Write a C++ program to check whether a given string is a subsequence of another given string. Return 1 for true and 0 for false.

What is the difficulty level of this exercise?