
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Maximize Count of 3-Length Palindromic Subsequences
In this article, we are going to delve into an interesting problem related to string manipulation and dynamic programming in various programming languages. The problem we're discussing today is "Maximize the count of 3-length palindromic subsequences with each index part of a single subsequence".
Problem Statement
Given a string, the task is to find the maximum count of 3-length palindromic subsequences such that each index in the string is a part of a single subsequence.
A 3-length palindromic subsequence is a subsequence of the form "aba", where 'a' and 'b' are any characters.
Solution Approach
To solve this problem, we'll count the frequency of each character in the string. We'll then select the character that appears most frequently. We'll form as many 3-length palindromic subsequences as possible with this character. Each subsequence will consist of the selected character, any other character, and the selected character again.
Example
Here're the programs that solves this problem
#include <stdio.h> #include <string.h> #include <stdlib.h> // Include for malloc #define CHAR_MAX 256 // Maximum possible characters in the ASCII character set // Function to find the maximum count of 3-length palindromic subsequences in the input string int maxPalindromeSubsequences(char* str) { int* count = (int*)malloc(sizeof(int) * CHAR_MAX); // Dynamically allocate memory for the array // Initialize the count array to 0 for (int i = 0; i < CHAR_MAX; i++) { count[i] = 0; } // Count the occurrences of each character in the string for (int i = 0; i < strlen(str); i++) { count[str[i]]++; } int maxCount = 0; // Iterate through the count array to find the maximum count of 3-length palindromic subsequences for (int i = 0; i < CHAR_MAX; i++) { if (count[i] >= 2) { int subseqCount = count[i] / 2; if (subseqCount > maxCount) { maxCount = subseqCount; } } } free(count); // Free the dynamically allocated memory return maxCount; // Return the maximum count of 3-length palindromic subsequences } int main() { char str[] = "abcaaadcb"; int result = maxPalindromeSubsequences(str); printf("The maximum count of 3-length palindromic subsequences is: %d\n", result); return 0; }
Output
The maximum count of 3-length palindromic subsequences is: 2
#include <iostream> #include <string> #include <algorithm> using namespace std; int maxPalindromeSubsequences(string str) { const int CHAR_MAX = 256; int count[CHAR_MAX] = {0}; for (int i=0; i<str.size(); i++) { count[str[i]]++; } return *max_element(count, count + CHAR_MAX) / 2; } int main() { string str = "abcaaadcb"; int result = maxPalindromeSubsequences(str); cout << "The maximum count of 3-length palindromic subsequences is: " << result << endl; return 0; }
Output
The maximum count of 3-length palindromic subsequences is: 2
import java.util.Arrays; public class MaxPalindromeSubsequences { // Function to find the maximum count of 3-length palindromic subsequences in the input string public static int maxPalindromeSubsequences(String str) { final int CHAR_MAX = 256; // Maximum possible characters in the ASCII character set int[] count = new int[CHAR_MAX]; // Array to store the count of each character in the string // Count the occurrences of each character in the string for (int i = 0; i < str.length(); i++) { count[str.charAt(i)]++; } int maxCount = 0; // Iterate through the count array to find the maximum count of 3-length palindromic subsequences for (int i = 0; i < CHAR_MAX; i++) { if (count[i] >= 2) { maxCount = Math.max(maxCount, count[i] / 2); } } return maxCount; // Return the maximum count of 3-length palindromic subsequences } public static void main(String[] args) { String str = "abcaaadcb"; int result = maxPalindromeSubsequences(str); System.out.println("The maximum count of 3-length palindromic subsequences is: " + result); } }
Output
The maximum count of 3-length palindromic subsequences is: 2
def max_palindrome_subsequences(s): CHAR_MAX = 256 # Maximum possible characters in the ASCII character set count = [0] * CHAR_MAX # List to store the count of each character in the string # Count the occurrences of each character in the string for char in s: count[ord(char)] += 1 max_count = 0 # Iterate through the count list to find the maximum count of 3-length palindromic subsequences for freq in count: if freq >= 2: max_count = max(max_count, freq // 2) return max_count # Return the maximum count of 3-length palindromic subsequences if __name__ == "__main__": s = "abcaaadcb" result = max_palindrome_subsequences(s) print("The maximum count of 3-length palindromic subsequences is:", result)
Output
The maximum count of 3-length palindromic subsequences is: 2
Explanation with a Test Case
Let's consider the string "abcaaadcb".
When this string is passed to the maxPalindromeSubsequences function, it first counts the frequency of each character in the string: {'a': 4, 'b': 2, 'c': 2, 'd': 1}.
It then finds the character that appears most frequently, which is 'a' with a frequency of 4.
To maximize the count of 3-length palindromic subsequences, it forms as many subsequences as possible with the character 'a'. Each subsequence consists of 'a', any other character, and 'a' again.
Since 'a' appears 4 times, it can form 2 such subsequences, "aba" and "aca".
So, the function returns 2.
Conclusion
This problem showcases how we can solve complex string manipulation problems using frequency counts and selection strategies. It's a fantastic problem to practice and improve your C++ coding skills.