Given a string str consisting of lower case alphabets only and an integer K. The task is to find the minimum cost to modify the string such that ascii value difference between any two characters of the given string is less than equal to K.
Following operations can be performed on the string:
- Increasing a character: For example, when you increase ‘a’ by 1 it becomes ‘b’. Similarly, if you increase ‘z’ by 1 it becomes ‘a’. Increment is done in cyclic manner.
- Decreasing a character: For example, when you decrease ‘a’ by 1 it becomes ‘z’. Similarly, if you decrease ‘z’ by 1 it becomes ‘y’. Decrement is done in cyclic manner.
Input: str = “abcd”, K = 1
Change ‘a’ to ‘b’ with cost 1 and ‘d’ to ‘c’ again with cost 1.
Total cost = 1 + 1 = 2.
Modified string will be “bbcc”
Input: str = “abcdefghi”, K = 2
Approach: The idea is to maintain hash table for all the characters of string to reduce time complexity instead of taking one character at a time. We need to check all the windows containing k contiguous characters and find the minimum cost among all the windows for modifying all the characters of string
The cost of modification of a character falls under the following categories:
- Case 1: If the characters are within window it will not incur any cost for modifying it.
- Case 2: If the window is in between the characters a-z and the character is on the left side of window.
If we increase the character which will incur a cost of d1 or if we decrease the character which will incur a cost of d2+d3 we find minimum of d1 and d2+d3.
If the window is in between the characters a-z and the character is on the right side of window.
If we decrease the character which will incur a cost of d1 or if we increase the character which will incur a cost of d2+d3 we find minimum of d1 and d2+d3
- Case 3: If the window is a sub window of characters ending at z and starting from a and if the character is outside the window.
If we increase the character it will incur a cost of d1 and if we decrease the character it will incur a cost of d2 we find minimum among d1 and d2.
Below is the implementation of the above approach:
Time Complexity: O(n)
Auxiliary Space: O(n)
- Minimum cost to construct a string
- Minimum cost to convert string into palindrome
- Minimum cost to traverse from one index to another in the String
- Minimum cost to make a string free of a subsequence
- Modify the string such that it contains all vowels at least once
- Modify string by removing vowels in between two consonants
- Modify the string by swapping continuous vowels or consonants
- Minimum cost to make two strings same
- Minimum Cost To Make Two Strings Identical
- Minimum cost to convert str1 to str2 with the given operations
- Minimum Cost to make two Numeric Strings Identical
- Minimum cost to make two strings identical by deleting the digits
- Minimum cost to sort strings using reversal operations of different costs
- Cost to make a string Panagram | Set 2
- Cost to make a string Panagram
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.