Super Reduced String


Problem Statement :


Reduce a string of lowercase characters in range ascii[‘a’..’z’]by doing a series of operations. In each operation, select a pair of adjacent letters that match, and delete them.

Delete as many characters as possible using this method and return the resulting string. If the final string is empty, return Empty String


Function Description

Complete the superReducedString function in the editor below.

superReducedString has the following parameter(s):

string s: a string to reduce
Returns

string: the reduced string or Empty String


Input Format

A single string, s.

Constraints

1  <=  len of s  <=  100


Sample Input 0

aaabccddd
Sample Output 0

abd



Solution :



title-img


                            Solution in C :

In   C++   :







#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<cstdio>
#include<numeric>
#include<cstring>
#include<ctime>
#include<cstdlib>
#include<set>
#include<map>
#include<unordered_map>
#include<unordered_set>
#include<list>
#include<cmath>
#include<bitset>
#include<cassert>
#include<queue>
#include<stack>
#include<deque>
#include<cassert>
using namespace std;
typedef long long ll;
typedef long double ld;
int main()
{
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	string s;
	cin >> s;
	vector<char>st;
	for (int i = 0; i < (int)s.length(); i++)
	{
		if (!st.empty() && st.back() == s[i])
		{
			st.pop_back();
		}
		else
		{
			st.push_back(s[i]);
		}
	}
	if (st.empty())
	{
		printf("Empty String\n");
	}
	else
	{
		for (int i = 0; i < (int)st.size(); i++)
		{
			printf("%c", st[i]);
		}
		printf("\n");
	}
	return 0;
}








In    Java   :





import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
      
        Scanner in = new Scanner(System.in);
        String S = in.nextLine();
        boolean changed = false;
        do{
            changed = false;
            for (int i = 0; i < S.length(); i++){
                if (i == S.length() - 1) continue;
                if (S.charAt(i) == S.charAt(i+1)){
                    changed = true;
                    S = S.substring(0,i) + S.substring(i+2);
                }
            }
        }while(changed);
        if (S.equals("")) System.out.println("Empty String");
        else System.out.println(S);
    }
}








In    C  :






#include<stdio.h>
typedef unsigned u;
char S[111];u l=1;
int main()
{
	char c;
	while((c=getchar())<'a');
	do
	{
		S[l]=c;
		if(S[l]==S[l-1])--l;
		else++l;
	}
	while((c=getchar())>='a');
	S[l]='\0';
	printf("%s\n",l-1?S+1:"Empty String");
	return 0;
}








In   Python3 :






s = input()
while True:
    for i in range(len(s)-1):
        if s[i] == s[i+1]:
            s = s[:i]+s[i+2:]
            break
    else:
        break
print(s if s else "Empty String")
                        








View More Similar Problems

Tree: Postorder Traversal

Complete the postorder function in the editor below. It received 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's postorder traversal as a single line of space-separated values. Input Format Our test code passes the root node of a binary tree to the postorder function. Constraints 1 <= Nodes in the tree <= 500 Output Format Print the

View Solution →

Tree: Inorder Traversal

In this challenge, you are required to implement inorder traversal of a tree. Complete the inorder function in your editor below, which has 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's inorder traversal as a single line of space-separated values. Input Format Our hidden tester code passes the root node of a binary tree to your $inOrder* func

View Solution →

Tree: Height of a Binary Tree

The height of a binary tree is the number of edges between the tree's root and its furthest leaf. For example, the following binary tree is of height : image Function Description Complete the getHeight or height function in the editor. It must return the height of a binary tree as an integer. getHeight or height has the following parameter(s): root: a reference to the root of a binary

View Solution →

Tree : Top View

Given a pointer to the root of a binary tree, print the top view of the binary tree. The tree as seen from the top the nodes, is called the top view of the tree. For example : 1 \ 2 \ 5 / \ 3 6 \ 4 Top View : 1 -> 2 -> 5 -> 6 Complete the function topView and print the resulting values on a single line separated by space.

View Solution →

Tree: Level Order Traversal

Given a pointer to the root of a binary tree, you need to print the level order traversal of this tree. In level-order traversal, nodes are visited level by level from left to right. Complete the function levelOrder and print the values in a single line separated by a space. For example: 1 \ 2 \ 5 / \ 3 6 \ 4 F

View Solution →

Binary Search Tree : Insertion

You are given a pointer to the root of a binary search tree and values to be inserted into the tree. Insert the values into their appropriate position in the binary search tree and return the root of the updated binary tree. You just have to complete the function. Input Format You are given a function, Node * insert (Node * root ,int data) { } Constraints No. of nodes in the tree <

View Solution →