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:
- Count of distinct permutations of length N having no similar adjacent characters
- Maximum Bitwise AND pair (X, Y) from given range such that X and Y can be same
- Length of diagonal of a parallelogram using adjacent sides and angle between them
- Length of longest subsequence consisting of distinct adjacent elements
- Loss when two items are sold at same price and same percentage profit/loss
- Check if lowercase and uppercase characters are in same order
- Length of largest subsequence consisting of a pair of alternating digits
- Strings formed from given characters without any consecutive repeating characters
- Min operations to reduce N by multiplying by any number or taking square root
- Create a new string by alternately combining the characters of two halves of the string in reverse
- Minimal moves to form a string by adding characters or appending string itself
- Length of Diagonals of a Cyclic Quadrilateral using the length of Sides.
- Length of a Diagonal of a Parallelogram using the length of Sides and the other Diagonal
- Check if two same sub-sequences exist in a string or not
- Check if a binary string contains consecutive same or not
- Check whether the string can be printed using same row of qwerty keypad
- Searching characters and substring in a String in Java
- Convert characters of a string to opposite case
- Generate a string of size N whose each substring of size M has exactly K distinct characters
- Sort string of characters using Stack
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.