Super Reduced String – HackerRank Solution

In this post, we will solve Super Reduced String HackerRank Solution. This problem (Super Reduced String) is a part of HackerRank Problem Solving series.

Task

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

Example.

s = ‘aab’

aab shortens to b in one operation: remove the adjacent a characters.

s = ‘abba’

Remove the two ‘b’ characters leaving ‘aa’. Remove the two ‘a’ characters to leave ”. 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 <= length of s <= 100

Sample Input 0

aaabccddd

Sample Output 0

abd

Explanation 0

Perform the following sequence of operations to get the final string:

aaabccddd → abccddd → abddd → abd

Sample Input 1

aa

Sample Output 1

Empty String

Explanation 1

aa → Empty String

Sample Input 2

baab

Sample Output 2

Empty String

Explanation 2

baab → bb → Empty String

Solution – Super Reduced String – HackerRank Solution

C++

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;


int main() {

    string S;
    cin >> S;

    bool isZero(false);
    
    for (int i = 0; i < (S.length() - 1); ++i)
        if (S[i] == S[i + 1])
        {
            S.erase(S.begin() + i);
            S.erase(S.begin() + i);

            if (S.length() == 0)
            {
                isZero = true;
                break;
            }
        
            i = -1;
        }

    if (isZero)
        cout << "Empty String";
    else
        cout << S;

    return 0;
}

Python

import re

inp=input().strip()
regex=re.compile(r'(\D)(\1)',re.M|re.I)
search=regex.search(inp)
while search:
    inp=inp.replace(search.group(),'')
    search=regex.search(inp)
print(inp if inp else 'Empty String')

Java

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

public class Solution {

    private static String super_reduced_string(String s){
        Stack<Character> stack = new Stack<>();
        for(char ch: s.toCharArray())
        {
            if(!stack.isEmpty() && stack.peek()==ch)
                stack.pop();
            else
                stack.push(ch);
        }
        if(stack.isEmpty())
            return "Empty String";
        else
        {
            StringBuilder sb = new StringBuilder();
            while(!stack.isEmpty())
            {
                sb.append(stack.peek());
                stack.pop();
            }
            sb.reverse();
            String result = String.valueOf(sb);
            return result;
        }
        
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String s = in.next();
        String result = super_reduced_string(s);
        System.out.println(result);
    }
}

Note: This problem (Super Reduced String) is generated by HackerRank but the solution is provided by CodingBroz. This tutorial is only for Educational and Learning purpose.

Leave a Comment

Your email address will not be published. Required fields are marked *