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)
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.
- Find the player to last modify a string such that even number of consonants and no vowels are left in the string
- Modify string by removing vowels in between two consonants
- Modify the string such that it contains all vowels at least once
- Modify the string by swapping continuous vowels or consonants
- Modify the string such that every character gets replaced with the next character in the keyboard
- Modify string by increasing each character by its distance from the end of the word
- Minimum cost to convert string into palindrome
- Minimum cost to construct a string
- Minimum cost to make a string free of a subsequence
- Minimum cost to traverse from one index to another in the String
- Minimum Cost of deletions such that string does not contains same consecutive characters
- Minimum cost to remove the spaces between characters of a String by rearranging the characters
- Minimum cost to convert given string to consist of only vowels
- Minimum Cost To Make Two Strings Identical
- Minimum cost to sort strings using reversal operations of different costs
- Minimum cost to make two strings identical by deleting the digits
- Minimum Cost to make two Numeric Strings Identical
- Minimum cost to make two strings same
- Minimum cost to convert str1 to str2 with the given operations
- Minimum cost required to convert all Subarrays of size K to a single element
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.