
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
Minimum Characters to Remove Palindromic Substrings in Ternary String
A palindromic string is a string that is equal to its reverse string. We are given a string that contains ?0', ?1', and ?2' and an array Q of length N and each index of the given array indicates a range in the form of pairs. We have to find the minimum number of characters that are needed to replace in the given range such that none of the palindromic substrings remains in that range.
Sample Example
Input1: string s: "01001020002", int Q = {{0,4}, {2,5}, {5,10}};
Output: 1 1 3
Explanation
For the range 0 to 4 we have two palindromes 010 and 1001 and we can change index 2 to ?2' and there will be no palindrome left.
For the range 2 to 5, we have only one palindrome that is 010 and can be changed by changing the first zero to 2.
For the range 5 to 10, we have palindromes 020, 000, and 20002. We can change the first 2 to ?1', just the next index ?0' to ?2', and the second last index value to ?1' to remove all the palindromes.
Naive Approach
In this approach, the idea is to change get all the combinations of the given range and find the one with no palindrome present with the minimum number of changes required. But the issue is the time complexity.
To implement this approach, we have to perform recursive calls, and traverse over the string. At each of the indexes we have three choices makes the time complexity to just get all the strings is 3^N. Now, we have to answer the Q queries and for each case, we have to check for the removal of the palindromes makes the time complexity O(Q*N*(3^N)).
For the recursive calls we have to maintain the space of N, which means space complexity is O(N).
Dynamic Programming
Idea
The idea behind this problem is, we don't require any palindrome in the given range and the smallest possible palindrome is of the length 2 for even length and 3 for the odd length.
We have three different characters, we need them all to make the given string palindrome free. There are a total of size choices or sequences by which we can form the sequence in such a way that there will be no palindrome present and that sequences are the permutations of the string ?012'.
We will make the dp array or vector to store all the possible cases with each sequence and which will give less number of characters we will use that sequence.
Implementation
In the implementation part, first, we will create a function that will take a string, sequence, DP vector, and the number of sequences as the parameters and will update the DP vector.
In this function, first, we will update the value of the first index, and then for every missed match we will update the value of the DP vector's current index.
We will create another function, in that we will manually type all the possible sequences and will store them in the array and will create a DP vector.
We will call to the above function for pre-processing by passing the values and later answer each query by traversing over them one by one.
Example
#include <bits/stdc++.h> #define SIZE 100005 using namespace std; // function to get the pre-processing of the results void preprocess(string& str, string& seq, vector<vector<int>>&dp, int n, int i){ dp[i][0] = (str[0] != seq[0]); // initialzie dp vector for (int j = 1; j < n; j++) { // storing the results if matched then adding zero otherwise one dp[i][j] = dp[i][j - 1] + (str[j] != seq[j % 3]); } return; } // function to find the number of changes required for each query void changesReq(string str, vector<pair<int, int>> Q){ int len = str.length(); // size of the given string int q = Q.size(); // number of queries vector<vector<int>>dp(6,vector<int>(len)); // dp vector to store the states results // vector to store each possible non-palindromic sequency vector<string> seq= { "012", "021", "102", "120", "201", "210" }; for (int i = 0; i < 6; i++){ // for each sequence storing state results in vector preprocess(str, seq[i], dp, len, i); } // getting all the queries for (int i = 0; i < q; i++){ int l = Q[i].first; int r = Q[i].second; int ans = INT_MAX; // finding the minimum cost amount for (int j = 0; j < 6; j++) { // Find the cost ans = min(ans, dp[j][r] - dp[j][l] + (str[l] != seq[j][l%3])); } cout <<ans<<" "; } } int main(){ string str = "01001020002"; vector<pair<int, int>>Q = {{0,4}, {2,5}, {5,10}}; // calling the function cout<<"The minimum characters to be replaced in the Ternary string to remove all palindromic substrings for Q queries is "<<endl; changesReq(str, Q); return 0; }
Output
The minimum characters to be replaced in the Ternary string to remove all palindromic substrings for Q queries is 1 1 3
Time and Space Complexity
The time complexity of the above code is O(N + Q), where N is the number of characters in the string and Q is the number of queries.
The space complexity of the above code is O(N), as we are storing the states in the vector of size N.
Conclusion
In this tutorial, we have implemented a code to find the minimum number of characters to change in the given range for some queries such that no palindromic string lefts. We have implemented the code using the concepts of the dynamic programming with the time complexity of the O(N+Q) and space complexity of O(N).