Minimize a binary string by repeatedly removing even length substrings of same characters
Last Updated :
21 May, 2021
Given a binary string str of size N, the task is to minimize the length of given binary string by removing even length substrings consisting of sam characters, i.e. either 0s or 1s only, from the string any number of times. Finally, print the modified string.
Examples:
Input: str =”101001″
Output: “10”
Explanation: The string can be minimized in the following manner: “101001″ -> “1011” -> “10”.
Input: str = “00110”
Output: “0”
Explanation: The string can be minimized in the following manner: “00110″ -> “110″ -> “0”.
Approach: The idea is to use a stack to solve the problem. While traversing the string, if the current character is found to be same as the top element of the stack, pop the element from the stack. After traversal, print the stack from bottom to top. Follow the steps below to solve the problem:
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
void PrintStack(stack< char > s)
{
if (s.empty())
return ;
char x = s.top();
s.pop();
PrintStack(s);
cout << x;
s.push(x);
}
void minString(string s)
{
stack< char > Stack;
Stack.push(s[0]);
for ( int i = 1; i < s.size(); i++) {
if (Stack.empty()) {
Stack.push(s[i]);
}
else {
if (Stack.top() == s[i]) {
Stack.pop();
}
else {
Stack.push(s[i]);
}
}
}
PrintStack(Stack);
}
int main()
{
string str = "101001" ;
minString(str);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void PrintStack(Stack<Character> s)
{
if (s.isEmpty())
return ;
char x = s.peek();
s.pop();
PrintStack(s);
System.out.print(x);
s.add(x);
}
static void minString(String s)
{
Stack<Character> Stack = new Stack<Character>();
Stack.add(s.charAt( 0 ));
for ( int i = 1 ; i < s.length(); i++) {
if (Stack.isEmpty()) {
Stack.add(s.charAt(i));
}
else {
if (Stack.peek() == s.charAt(i)) {
Stack.pop();
}
else {
Stack.push(s.charAt(i));
}
}
}
PrintStack(Stack);
}
public static void main(String[] args)
{
String str = "101001" ;
minString(str);
}
}
|
Python3
def PrintStack(s) :
if ( len (s) = = 0 ) :
return ;
x = s[ - 1 ];
s.pop();
PrintStack(s);
print (x, end = "");
s.append(x);
def minString(s) :
Stack = [];
Stack.append(s[ 0 ]);
for i in range ( 1 , len (s)) :
if ( len (Stack) = = 0 ) :
Stack.append(s[i]);
else :
if (Stack[ - 1 ] = = s[i]) :
Stack.pop();
else :
Stack.append(s[i]);
PrintStack(Stack);
if __name__ = = "__main__" :
string = "101001" ;
minString(string);
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static void PrintStack(Stack< char > s)
{
if (s.Count == 0)
return ;
char x = s.Peek();
s.Pop();
PrintStack(s);
Console.Write(( char )x);
s.Push(x);
}
static void minString(String s)
{
Stack< char > Stack = new Stack< char >();
Stack.Push(s[0]);
for ( int i = 1; i < s.Length; i++)
{
if (Stack.Count == 0)
{
Stack.Push(s[i]);
}
else
{
if (Stack.Peek() == s[i])
{
Stack.Pop();
}
else
{
Stack.Push(s[i]);
}
}
}
PrintStack(Stack);
}
public static void Main(String[] args)
{
String str = "101001" ;
minString(str);
}
}
|
Javascript
<script>
function PrintStack( s)
{
if (s.length == 0)
return ;
let x = s[s.length-1];
s.pop();
PrintStack(s);
document.write(x);
s.push(x);
}
function minString( s)
{
let Stack = [];
Stack.push(s[0]);
for (let i = 1; i < s.length; i++) {
if (Stack.length==0) {
Stack.push(s[i]);
}
else {
if (Stack[Stack.length-1] == s[i]) {
Stack.pop();
}
else {
Stack.push(s[i]);
}
}
}
PrintStack(Stack);
}
let str = "101001" ;
minString(str);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...