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
- Searching characters and substring in a String in Java
- Missing characters to make a string Pangram
- Convert characters of a string to opposite case
- Program to check if first and the last characters of string are equal
- How to remove all non-alphanumeric characters from a string in Java
- Contiguous subsegments of a string having distinct subsequent characters
- Program to find the XOR of ASCII values of characters in a string
- Check if the characters in a string form a Palindrome in O(1) extra space
- Program to count vowels, consonant, digits and special characters in string.
- C program to find the length of a string
- Find longest length number in a string
- Python program to print even length words in a string
- Generate a string of size N whose each substring of size M has exactly K distinct characters
- Strings formed from given characters without any consecutive repeating characters
- Min operations to reduce N by multiplying by any number or taking square root
- Print a pair of numbers with the given Sum and Product
- Co-prime pair with given sum minimum difference