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
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); });
the pytho solution doesn' work for me
changed = True
while changed and s != "":
changed = False
for i in range(1,len(s) – 1):
if s[i] == s[i+1]:
changed = True
s = s[:(i)] + s[(i+2):]
break
print(s)
if s[0] == s[1]:
s = s[2:]
return s if s else 'Empty String'
#python code
def superReducedString(s):
stack = []
for char in s:
if stack and stack[-1] == char:
stack.pop()
else:
stack.append(char)
reduced_string = ''.join(stack)
if reduced_string == "":
return 'Empty String'
else:
return reduced_string