Given two string X and Y of the same length which consists of lowercase letters and also an integer K. The task is to find the maximum length up to which X can be changed to Y within the given cost K.
The cost of changing a character is given by the absolute difference between the ASCII value of the characters. That is, to change a character at index i, cost = |x[i] – Y[i]|
Input: X = abcd, Y = bcde, K = 3
Explanation: A maximum of 3 characters can be changed because the cost to change each article is 1.
Approach: The idea is to maintain a prefix array of length N to store the absolute sum of the strings. That is, the cost to change the string X to Y. The following steps can be followed to compute the result:
- Maintain two pointers say i and j.
- In a while loop check if the difference between ith index and jth index of prefix array is greater than given cost or not.
- If the difference is greater than given cost then increase the j pointer to compensate for the cost else increase the i pointer.
Below is the implementation of the above approach:
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.
- Length of the largest substring which have character with frequency greater than or equal to half of the substring
- Longest sub-string having frequency of each character less than equal to k
- Longest subarray with absolute difference between elements less than or equal to K using Heaps
- Length of longest subarray in which elements greater than K are more than elements not greater than K
- Longest subsequence whose average is less than K
- Longest suffix such that occurrence of each character is less than N after deleting atmost K characters
- Count of alphabets having ASCII value less than and greater than k
- Count the number of words having sum of ASCII values less than and greater than k
- Sum of all array elements less than X and greater than Y for Q queries
- Longest substring with count of 1s more than 0s
- For each element in 1st array count elements less than or equal to it in 2nd array
- Count elements less than or equal to a given value in a sorted rotated array
- Maximum sum subarray having sum less than or equal to given sum
- Count sub-arrays which have elements less than or equal to X
- Find maximum number of elements such that their absolute difference is less than or equal to 1
- Minimum swaps required to bring all elements less than or equal to k together
- Find maximum sum array of length less than or equal to m
- For each element in 1st array count elements less than or equal to it in 2nd array | Set 2
- Maximum product from array such that frequency sum of all repeating elements in product is less than or equal to 2 * k
- Sum of elements in 1st array such that number of elements less than or equal to them in 2nd array is maximum
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.