Given a string str and two integers X and Y, the task is to find the maximum cost required to remove all the substrings “pr” and “rp” from the given string, where removal of substrings “rp” and “pr” costs X and Y respectively.
Input: str = “abppprrr”, X = 5, Y = 4
Following operations are performed:
“abppprrr” -> “abpprr”, cost = 5
“abpprr” -> “abpr”, cost = 10
“abpr” -> “ab”, cost = 15
Therefore, the maximized cost is 15
Input: str = “prprprrp”, X = 7, Y = 10
Approach: The problem can be solved using the Greedy Approach. The idea here is to remove “pr” if X is greater than Y or remove “rp” otherwise. Follow the steps below to solve the problem.
- If X < Y: Swap the value of X and Y and replace the character ‘p’ to ‘r’ and vice versa in the given string.
- Initialize two variables countP and countR to store the count of ‘p’ and ‘r’ in the string respectively.
- Iterate over the array arr and perform the steps below:
- If str[i] = ‘p’: Increment the countP by 1.
- If str[i] = ‘r’: Check the value of countP. If countP > 0, then increment the result by X and decrement the value of countP by 1. Otherwise, increment the value of countR by 1.
- If str[i] != ‘p’ and str[i]!=’r’: Increment the result by min(countP, countR) * Y.
- Increment the result by min(countP, countR) * Y.
- Finally, after the removal of all the required substrings, print the result obtained.
Time Complexity:O(N), where N denotes the length of the string
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Cost required to empty a given array by repeated removal of maximum obtained by given operations
- Check if a palindromic string can be obtained by concatenating substrings split from same indices of two given strings
- Minimize cost to convert given string into concatenation of equal substrings of length K
- Maximize removal of adjacent array elements based on their absolute value
- Maximize length of Subarray of 1's after removal of a pair of consecutive Array elements
- Maximize difference between the Sum of the two halves of the Array after removal of N elements
- Maximize sum of remaining elements after every removal of the array half with greater sum
- Rearrange the string to maximize the number of palindromic substrings
- Maximize cost of deletions to obtain string having no pair of similar adjacent characters
- Check if a string can be obtained by rotating another string 2 places
- Check if a string can be obtained by rotating another string d places
- String obtained by reversing and complementing a Binary string K times
- Count of integers obtained by replacing ? in the given string that give remainder 5 when divided by 13
- Kth character from the Nth string obtained by the given operations
- Maximize partitions such that no two substrings have any common character
- Minimum cost to empty Array where cost of removing an element is 2^(removed_count) * arr[i]
- Maximum length of balanced string after swapping and removal of characters
- Lexicographically largest possible String after removal of K characters
- Sort a String in decreasing order of values associated after removal of values smaller than X
- Longest palindromic string possible after removal of a substring
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.