Skip to content
Programming101
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programmingoneonone

Learn everything about programming

HackerRank Super Reduced String problem solution

YASH PAL, 31 July 2024

In this HackerRank Super Reduced String problem, you need to 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

HackerRank Super Reduced String problem solution

Topics we are covering

Toggle
  • Problem solution in Python programming.
  • Problem solution in Java Programming.
    • Problem solution in C++ programming.
    • Problem solution in C programming.
    • Problem solution in JavaScript programming.

Problem solution in Python programming.

s = input()

changed = True
while changed and s != "":
    changed = False
    for i in range(len(s) - 1):
        if s[i] == s[i+1]:
            changed = True
            s = s[:(i)] + s[(i+2):]
            break

if s == "":
    print('Empty String')
else:
    print(s)

Problem solution in Java Programming.

import java.io.*;
import java.util.*;

public class Solution {
	
	public static void main(String [] args){
		/*FileReader fr = null;
		try {
			fr = new FileReader("/Users/pkahrl/Documents/eclipse-workspace/Comp/src/kahrl/prob1/input.txt");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		Scanner in = new Scanner(fr);*/
		Scanner in = new Scanner(System.in);
		reduceString(in.nextLine());
		
		
		
	}
	
	static void reduceString(String s) {
		boolean found = false;
		for (int i = 0; i < s.length() - 1; i++) {
			String a = s.substring(i, i + 1);
			String b = s.substring(i + 1, i + 2);
			if (a.equals(b)) {
				s = s.substring(0, i) + s.substring(i + 2, s.length());

				reduceString(s);
				found = true;
				break;
			}
		}
        if(!found){
        	if(s.isEmpty()){
        		System.out.println("Empty String");
        	} else {
		        System.out.println(s);
        	}
        }

	}

}

Problem solution in C++ programming.

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

char s[107];

bool redukuj() {
	int n = 0;
	for(; s[n]; ++n);
	for(int i = 0; s[i + 1]; ++i) {
		if(s[i] == s[i + 1]) {
			for(int j = i; s[j + 2]; ++j) {
				s[j] = s[j + 2];
			}
			s[n - 1] = s[n - 2] = 0;
			return true;
		}
	}
	return false;
}

int main() {
	ios_base::sync_with_stdio(0);
	cin >> s;
	while(redukuj());
	if(!s[0]) cout << "Empty Stringn";
	else cout << s << endl;
	return 0;
}

Problem solution in C programming.

#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#define MAX_LENGTH 101

int main() {

    /* Enter your code here. Read input from STDIN. Print output to STDOUT */
    char str[MAX_LENGTH];
    fgets(str,MAX_LENGTH,stdin);
    
    int length = strlen(str);
    
    if(length < 2){
        printf("Empty Stringn");
        return 0;
    }
    
    
    bool done = false;
    while(!done){
        done = true;
        if(strlen(str) < 2) break;
        
        for(int i =0; i < strlen(str)-1; i++){
            if(str[i] ==  str[i+1]){
                done = false;
                memmove(&str[i],&str[i+2],strlen(str)-i-1);
                break;
            }
        }    
    }
    if(strlen(str)){
      printf("%sn",str);
      return 0;
    } 
    printf("Empty Stringn");
    return 0;
}

Problem solution in JavaScript programming.

function processData(input) {
    //Enter your code here
  let arr = input.split('');
  for(let i = 0; i< arr.length; ++i) { 
    if(arr[i] === arr[i+1]) {
      arr.splice(i, 2);
      i = -1;
    }
  }
  if(arr.length === 0) console.log('Empty String');
  console.log(arr.join(''));
} 

process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
    _input += input;
});

process.stdin.on("end", function () {
   processData(_input);
});

Algorithms coding problems solutions

Post navigation

Previous post
Next post
  • Automating Image Format Conversion with Python: A Complete Guide
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes