Created
May 24, 2017 12:40
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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