Reduce string to shortest length by deleting a pair of same adjacent characters
Last Updated :
13 Mar, 2023
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.
Examples:
Input: str = "aaabccddd"
Output: 3
Following are sequence of operations:
aaabccddd -> abccddd -> abddd -> abd
Input: str = "aa"
Output: 1
Approach:
- 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:
C++
// C++ program to count deletions
// to reduce the string to its shortest
// length by deleting a pair of
// same adjacent characters
#include <bits/stdc++.h>
using namespace std;
// Function count the operations
int reduceString(string s, int l)
{
int count = 1, steps = 0;
// traverse in the string
for (int i = 1; i < l; i++) {
// if adjacent characters are same
if (s[i] == s[i - 1])
count += 1;
else {
// if same adjacent pairs are more than 1
steps += (count / 2);
count = 1;
}
}
steps += count / 2;
return steps;
}
// Driver Code
int main()
{
string s = "geeksforgeeks";
int l = s.length();
cout << reduceString(s, l) << endl;
return 0;
}
Java
// Java program to count deletions
// to reduce the string to its
// shortest length by deleting a
// pair of same adjacent characters
import java.io.*;
import java.util.*;
import java.lang.*;
class GFG
{
// Function count
// the operations
static int reduceString(String s,
int l)
{
int count = 1, steps = 0;
// traverse in the string
for (int i = 1; i < l; i++)
{
// if adjacent characters
// are same
if (s.charAt(i) == s.charAt(i - 1))
count += 1;
else
{
// if same adjacent pairs
// are more than 1
steps += (count / 2);
count = 1;
}
}
steps += count / 2;
return steps;
}
// Driver Code
public static void main(String[] args)
{
String s = "geeksforgeeks";
int l = s.length();
System.out.print(reduceString(s, l) + "\n");
}
}
Python3
# Python3 program to count
# deletions to reduce
# the string to its
# shortest length by
# deleting a pair of
# same adjacent characters
# Function count
# the operations
def reduceString(s, l):
count = 1;
steps = 0;
# traverse in
# the string
for i in range(1,l):
# if adjacent
# characters are same
if (s[i] is s[i - 1]):
count += 1;
else:
# if same adjacent pairs
# are more than 1
steps +=(int)(count / 2);
count = 1;
steps +=(int)(count / 2);
return steps;
# Driver Code
s = "geeksforgeeks";
l = len(s);
print(reduceString(s, l));
# This code contributed by Rajput-Ji
C#
// C# program to count deletions
// to reduce the string to its
// shortest length by deleting a
// pair of same adjacent characters
using System;
class GFG
{
// Function count
// the operations
static int reduce(string s,
int l)
{
int count = 1, step = 0;
// traverse in
// the string
for (int i = 1; i < l; i++)
{
// if adjacent characters
// are same
if (s[i] == s[i - 1])
count += 1;
else
{
// if same adjacent pairs
// are more than 1
step += (count / 2);
count = 1;
}
}
step += count / 2;
return step;
}
// Driver Code
public static void Main()
{
string s = "geeksforgeeks";
int l = s.Length;
Console.WriteLine(reduce(s, l));
}
}
// This code is contributed by
// Akanksha Rai(Abby_akku)
PHP
<?php
// PHP program to count
// deletions to reduce
// the string to its
// shortest length by
// deleting a pair of
// same adjacent characters
// Function count
// the operations
function reduceString($s, $l)
{
$count = 1;
$steps = 0;
// traverse in
// the string
for ($i = 1; $i < $l; $i++)
{
// if adjacent
// characters are same
if ($s[$i] == $s[$i - 1])
$count += 1;
else
{
// if same adjacent pairs
// are more than 1
$steps +=(int)($count / 2);
$count = 1;
}
}
$steps +=(int)($count / 2);
return $steps;
}
// Driver Code
$s = "geeksforgeeks";
$l = strlen($s);
echo reduceString($s, $l);
// This code is contributed by ajit
?>
JavaScript
<script>
// Javascript program to count deletions
// to reduce the string to its
// shortest length by deleting a
// pair of same adjacent characters
// Function count
// the operations
function reduce(s, l)
{
let count = 1, step = 0;
// Traverse in the string
for(let i = 1; i < l; i++)
{
// If adjacent characters
// are same
if (s[i] == s[i - 1])
count += 1;
else
{
// If same adjacent pairs
// are more than 1
step += parseInt(count / 2, 10);
count = 1;
}
}
step += parseInt(count / 2, 10);
return step;
}
// Driver code
let s = "geeksforgeeks";
let l = s.length;
document.write(reduce(s, l));
// This code is contributed by mukesh07
</script>
Time Complexity: O(N), where N is the length of the given string.
Auxiliary Space: O(1)
Similar Reads
Minimize length of string by replacing K pairs of distinct adjacent characters Given string str of length N, the task is to find the minimum length to which the given string can be reduced by replacing any pair of non-equal adjacent characters with a single character at most K times. Examples: Input: str = "aabc", K =1Output: 3Explanation: Replace "bc" with a single character
5 min read
Transform string A into B by deleting characters from ends and reinserting at any position Given two strings A and B that are anagrams of each other, the task is to convert A to B if possible in minimum number of operations. An operation is defined as removing either the first or the last character in A and inserting it back anywhere in the string. Examples: Input: A = "edacb", B = "abcde
13 min read
Minimum replacements in a string to make adjacent characters unequal Given a lowercase character string str of size N. In one operation any character can be changed into some other character. The task is to find the minimum number of operations such that no two adjacent characters are equal.Examples: Input: Str = "caaab" Output: 1 Explanation: Change the second a to
6 min read
Smallest length string with repeated replacement of two distinct adjacent Given a string of any combination of three letters 'a', 'b', and 'c', find length of the smallest string that can be obtained by applying the following operation repeatedly: Take any two adjacent, distinct characters and replace them with the third. Examples: Input : cabOutput : 2We can select any t
15+ min read
Remove all duplicate adjacent characters from a string using Stack Given a string, str, the task is to remove all the duplicate adjacent characters from the given string. Examples: Input: str= âazxxzyâOutput: ay Removal of "xx" modifies the string to âazzyâ. Now, the removal of "zz" modifies the string to âayâ. Since the string âayâ doesn't contain duplicates, the
6 min read