Reduce string to shortest length by deleting a pair of same adjacent characters

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:



  1. Initialize count = 1 initially.
  2. Iterate for every character, increase count if s[i]==s[i-1].
  3. 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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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");
}
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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)

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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
?>

chevron_right


Output:

2


My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.



Improved By : jit_t, Akanksha_Rai, Rajput-Ji



Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.