Given a string str of lowercase characters. The task is to count the number of deletions required to reduce the string to its shortest length. In each delete operation, you can select a pair of adjacent lowercase letters that match, and then delete them. The task is to print the count of deletions done.
Input: str = "aaabccddd" Output: 3 Following are sequence of operations: aaabccddd -> abccddd -> abddd -> abd Input: str = "aa" Output: 1
- Initialize count = 1 initially.
- Iterate for every character, increase count if s[i]==s[i-1].
- If s[i]!=s[i-1], add count/2 to the number of steps, and re-initialize count to 1.
If s[i]!=s[i-1], then the number of deletions is increased by count/2. If the count is even, number of pairs will be count/2. If count is odd, then the number of deletions will be (count-1)/2 which is the same as (int)count/2.
Below is the implementation of the above approach:
- Minimal moves to form a string by adding characters or appending string itself
- Create a new string by alternately combining the characters of two halves of the string in reverse
- Sort string of characters using Stack
- Program to check if first and the last characters of string are equal
- Missing characters to make a string Pangram
- Searching characters and substring in a String in Java
- Convert characters of a string to opposite case
- C program to find the length of a string
- Find longest length number in a string
- Check if the characters in a string form a Palindrome in O(1) extra space
- Python program to print even length words in a string
- Program to count vowels, consonant, digits and special characters in string.
- Co-prime pair with given sum minimum difference
- Arc length from given Angle
- Check if lowercase and uppercase characters are in same order
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.