Minimum characters to be replaced in Ternary string to remove all palindromic substrings for Q queries
Given a ternary string S of length N containing only ‘0’, ‘1’ and ‘2’ characters and Q queries containing a range of indices [L, R], the task for each query [L, R] is to find the minimum number of characters to convert to either ‘0’, ‘1’ or ‘2’ such that there exists no palindromic substring of length at least 2, between str[L] and str[R].
Examples:
Input: N = 10, Q = 3, S = “0200011011”, queries = {0, 4}, {1, 6}, {2, 8}
Output: 2 3 3
Explanation:
Query 1: {0, 4} ⇒ s = “02000” palindromic substrings present are “020”, “00”, “000”. Substring s can be changed to “02102” with 2 changes. “acbac” has zero palindromic substrings.
Query 2: {1, 6} ⇒ s = “200011” palindromic substrings present are “00”, “000”, “11”. Substring s can be changed to “201201″ with 3 changes. “cabcab” has zero palindromic substrings.
Query 3: {2, 8} ⇒ s = “aaabbab” palindromic substrings present are”00″, “000”, “0110”, “11”, “101”. Substring s can be changed to “1201201″ with 3 changes. “1201201” has zero palindromic substrings.Input: N = 5, Q = 2, S = “bccab”, queries = {0, 4}, {1, 2}
Output: 2 1
Naive Approach: The given problem can be solved by recursively modifying each character of the substring for a given query and checking if the resultant substring has palindromic substrings.
Time Complexity: O(Q*3N)
Auxiliary Space: O(N)
Efficient Approach: The given problem can be solved using Dynamic Programming, the idea is to preprocess the possible answers for all substrings using the following observations:
- A string has zero palindromic substrings if si ≠si-1 and si ≠si-2 i.e., no two adjacent characters are the same and no two alternative characters are the same for the following reasons:
- Even length palindromes are formed if adjacent characters are the same.
- Odd length palindromes are formed if alternate characters are the same.
- If the first character of the string is ‘0’, then the next character must be ‘1’ or ‘2’ (since si ≠si-1)
- If the second character is ‘1’, then the third character cannot be ‘0’ (since si ≠si-2) or ‘1’ (since si ≠si-1)
- Therefore the third character will be ‘2’. Then the fourth character can only be ‘1’. The string formed is “012012012..”
- Similarly, after permutating the characters ‘0’, ‘1’ and ‘2’ and repeating each permutation several times, we get the target strings “012012012…”, “120120120…”, “021021021…” etc
- There are six possible permutations with ‘0’, ‘1’ and ‘2’ characters and thus six target strings
Now, each query can be solved by transforming the substring from L to R character into the six target strings and checking which of them requires the least operations. This approach requires O(N) time for each query. The approach can be optimized further by preprocessing the given string. Following is the optimized solution:
- Let prefix[i] be the array that contains the minimum number of operations required to transform the string into the target string i. Therefore, prefix[i][j] is the number of operations required to transform the first j characters of the string to target string i.
- After the preprocessing of the above step, each query j can be solved in O(1) time using the formula for each possible preprocessed string as the minimum of currentCost and (prefix[i][rj] – prefix[i][lj – 1]) for all possible sequences and print the cost.
Below is the implementation of the above approach:
C++
// C++ program for the above approach #include <bits/stdc++.h> #define SIZE 100005 using namespace std; // Function to preprocess the cost of // converting the first j character to // each sequence prefix[i] void preprocess(string& s, string& t, int prefix[][SIZE], int n, int i) { // Initialize DP array prefix[i][0] = (s[0] != t[0]); for ( int j = 1; j < n; j++) { // prefix[i][j] defines minimum // operations to transform first j // characters of s into sequence i prefix[i][j] = prefix[i][j - 1] + (s[j] != t[j % 3]); } return ; } // Function to find the minimum number of // changes required to make each substring // between [L, R] non-palindromic void minChangesNonPalindrome( string str, int N, int Q, vector<pair< int , int > > queries) { // Initialize the DP array int prefix[6][SIZE]; // Initialize the 6 different patterns // that can be formed to make substrings // non palindromic vector<string> sequences = { "012" , "021" , "102" , "120" , "201" , "210" }; for ( int i = 0; i < 6; i++) { // Preprocess the string with // the ith sequence preprocess(str, sequences[i], prefix, N, i); } // Iterate through queries for ( int i = 0; i < Q; i++) { int l = queries[i].first + 1, r = queries[i].second + 1; int cost = INT_MAX; // Find the minimum operations by // comparing 6 different patterns // of the substrings for ( int j = 0; j < 6; j++) { // Find the cost cost = min( cost, prefix[j][r] - prefix[j][l] + (str[l] != sequences[j][l % 3])); } cout << cost << '\n' ; } } // Driver Code int main() { string S = "0200011011" ; vector<pair< int , int > > queries = { { 0, 4 }, { 1, 6 }, { 2, 8 } }; int N = S.length(); int Q = queries.size(); minChangesNonPalindrome( S, N, Q, queries); return 0; } |
Python3
# Python 3 program for the above approach import sys SIZE = 100005 # Function to preprocess the cost of # converting the first j character to # each sequence prefix[i] def preprocess(s, t, prefix, n, i): # Initialize DP array prefix[i][ 0 ] = (s[ 0 ] ! = t[ 0 ]) for j in range ( 1 , n): # prefix[i][j] defines minimum # operations to transform first j # characters of s into sequence i prefix[i][j] = prefix[i][j - 1 ] + (s[j] ! = t[j % 3 ]) return # Function to find the minimum number of # changes required to make each substring # between [L, R] non-palindromic def minChangesNonPalindrome( st, N, Q, queries): # Initialize the DP array prefix = [[ 0 for x in range (SIZE)] for y in range ( 6 )] # Initialize the 6 different patterns # that can be formed to make substrings # non palindromic sequences = [ "012" , "021" , "102" , "120" , "201" , "210" ] for i in range ( 6 ): # Preprocess the string with # the ith sequence preprocess(st, sequences[i], prefix, N, i) # Iterate through queries for i in range (Q): l = queries[i][ 0 ] + 1 r = queries[i][ 1 ] + 1 cost = sys.maxsize - 1 # Find the minimum operations by # comparing 6 different patterns # of the substrings for j in range ( 6 ): # Find the cost cost = min (cost, prefix[j][r] - prefix[j][l] + (st[l] ! = sequences[j][l % 3 ])) print (cost) # Driver Code if __name__ = = "__main__" : S = "0200011011" queries = [[ 0 , 4 ], [ 1 , 6 ], [ 2 , 8 ]] N = len (S) Q = len (queries) minChangesNonPalindrome( S, N, Q, queries) # This code is contributed by ukasp. |
Javascript
<script> // JavaScript Program to implement // the above approach let SIZE = 100005 // Function to preprocess the cost of // converting the first j character to // each sequence prefix[i] function preprocess(s, t, prefix, n, i) { // Initialize DP array prefix[i][0] = (s[0] != t[0]); for (let j = 1; j < n; j++) { // prefix[i][j] defines minimum // operations to transform first j // characters of s into sequence i prefix[i][j] = prefix[i][j - 1] + (s[j] != t[j % 3]); } return prefix; } // Function to find the minimum number of // changes required to make each substring // between [L, R] non-palindromic function minChangesNonPalindrome( str, N, Q, queries) { // Initialize the DP array let prefix = new Array(6); for (let i = 0; i < prefix.length; i++) prefix[i] = new Array(SIZE).fill(0); // Initialize the 6 different patterns // that can be formed to make substrings // non palindromic let sequences = [ "012" , "021" , "102" , "120" , "201" , "210" ]; for (let i = 0; i < 6; i++) { // Preprocess the string with // the ith sequence prefix = preprocess(str, sequences[i], prefix, N, i); } // Iterate through queries for (let i = 0; i < Q; i++) { let l = queries[i].first + 1, r = queries[i].second + 1; let cost = Number.MAX_VALUE; // Find the minimum operations by // comparing 6 different patterns // of the substrings for (let j = 0; j < 6; j++) { // Find the cost cost = Math.min( cost, prefix[j][r] - prefix[j][l] + (str[l] != sequences[j][l % 3])); } document.write(cost + '<br>' ); } } // Driver Code let S = "0200011011" ; let queries = [{ first: 0, second: 4 }, { first: 1, second: 6 }, { first: 2, second: 8 }]; let N = S.length; let Q = queries.length; minChangesNonPalindrome( S, N, Q, queries); // This code is contributed by Potta Lokesh </script> |
2 3 3
Time Complexity: O(N + Q)
Auxiliary Space: O(N)