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
- Sum of all possible strings obtained by removal of non-empty substrings
- Count binary strings of length same as given string after removal of substrings "01" and "00" that consists of at least one '1'
- 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 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
- Maximize length of Subarray of 1's after removal of a pair of consecutive Array elements
- 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
- Smallest string obtained by removing all occurrences of 01 and 11 from Binary String
- Check if a string can be obtained by appending subsequences of another string
- Check if string S2 can be obtained by appending subsequences of string S1
- Minimum removal of characters required such that permutation of given string is a palindrome
- Minimum removal of subsequences of distinct consecutive characters required to empty a given string
- Maximize cost to empty given array by repetitively removing K array elements
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.