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++
#include <bits/stdc++.h>
using namespace std;
int reduceString(string s, int l)
{
int count = 1, steps = 0;
for ( int i = 1; i < l; i++) {
if (s[i] == s[i - 1])
count += 1;
else {
steps += (count / 2);
count = 1;
}
}
steps += count / 2;
return steps;
}
int main()
{
string s = "geeksforgeeks" ;
int l = s.length();
cout << reduceString(s, l) << endl;
return 0;
}
|
Java
import java.io.*;
import java.util.*;
import java.lang.*;
class GFG
{
static int reduceString(String s,
int l)
{
int count = 1 , steps = 0 ;
for ( int i = 1 ; i < l; i++)
{
if (s.charAt(i) == s.charAt(i - 1 ))
count += 1 ;
else
{
steps += (count / 2 );
count = 1 ;
}
}
steps += count / 2 ;
return steps;
}
public static void main(String[] args)
{
String s = "geeksforgeeks" ;
int l = s.length();
System.out.print(reduceString(s, l) + "\n" );
}
}
|
Python3
def reduceString(s, l):
count = 1 ;
steps = 0 ;
for i in range ( 1 ,l):
if (s[i] is s[i - 1 ]):
count + = 1 ;
else :
steps + = ( int )(count / 2 );
count = 1 ;
steps + = ( int )(count / 2 );
return steps;
s = "geeksforgeeks" ;
l = len (s);
print (reduceString(s, l));
|
C#
using System;
class GFG
{
static int reduce( string s,
int l)
{
int count = 1, step = 0;
for ( int i = 1; i < l; i++)
{
if (s[i] == s[i - 1])
count += 1;
else
{
step += (count / 2);
count = 1;
}
}
step += count / 2;
return step;
}
public static void Main()
{
string s = "geeksforgeeks" ;
int l = s.Length;
Console.WriteLine(reduce(s, l));
}
}
|
PHP
<?php
function reduceString( $s , $l )
{
$count = 1;
$steps = 0;
for ( $i = 1; $i < $l ; $i ++)
{
if ( $s [ $i ] == $s [ $i - 1])
$count += 1;
else
{
$steps +=(int)( $count / 2);
$count = 1;
}
}
$steps +=(int)( $count / 2);
return $steps ;
}
$s = "geeksforgeeks" ;
$l = strlen ( $s );
echo reduceString( $s , $l );
?>
|
Javascript
<script>
function reduce(s, l)
{
let count = 1, step = 0;
for (let i = 1; i < l; i++)
{
if (s[i] == s[i - 1])
count += 1;
else
{
step += parseInt(count / 2, 10);
count = 1;
}
}
step += parseInt(count / 2, 10);
return step;
}
let s = "geeksforgeeks" ;
let l = s.length;
document.write(reduce(s, l));
</script>
|
Time Complexity: O(N), where N is the length of the given string.
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...