Skip to content

Instantly share code, notes, and snippets.

@ShaneRich5
Created May 24, 2017 12:40
Show Gist options
  • Save ShaneRich5/13d8cbc1269e60e007dda3bd09017392 to your computer and use it in GitHub Desktop.
Save ShaneRich5/13d8cbc1269e60e007dda3bd09017392 to your computer and use it in GitHub Desktop.
Solution to the Super Reduce String problem on Hackerrank. https://www.hackerrank.com/challenges/reduced-string
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
StringBuilder s = new StringBuilder(scan.nextLine());
int i = 0;
while (i < s.length() && s.length() > 1) {
while (i < s.length() - 1 && s.charAt(i) == s.charAt(i + 1)) {
s.deleteCharAt(i);
s.deleteCharAt(i);
if (i > 0) {
i--;
}
}
i++;
}
System.out.println((s.length() > 0) ? s : "Empty String");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment