Created
August 20, 2016 21:36
-
-
Save vvikramjhu/44afe4e9c2ba126c2b0962324cb306d2 to your computer and use it in GitHub Desktop.
This file contains 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
// https://www.hackerrank.com/challenges/reduced-string/submissions/code/26069930 | |
import java.io.*; | |
import java.util.*; | |
import java.text.*; | |
import java.math.*; | |
import java.util.regex.*; | |
public class Solution { | |
public void stringCondenser(String s, String previousCondensed){ | |
char previous = ' '; | |
char current; | |
boolean stringEnd = false; | |
String condensedString; | |
condensedString = "Empty String" ; | |
int counter = 0; | |
if (s.length() == 0){ | |
System.out.print(condensedString); | |
return; | |
} | |
if (s.length() == 1){ | |
condensedString = "" + s.charAt(0); | |
System.out.print(condensedString); | |
return; | |
} | |
for( int i = 0; i < s.length(); i++){ | |
current = s.charAt(i); | |
//System.out.print( " --- " + " counter: " + counter + " cndnsdString: " + condensedString + " previous: " + previous + " current: " + current + "\n"); | |
if (previous == ' '){ | |
previous = current ; | |
continue; | |
} | |
if ( i == s.length() -1 ){ | |
stringEnd = true; | |
} | |
if ( current == previous ) { | |
counter += 1; | |
if (stringEnd){ | |
if (counter % 2 == 0){ | |
if (condensedString == "Empty String" ){ | |
condensedString = ""; | |
} | |
condensedString = condensedString + previous; | |
} | |
} | |
//System.out.print( " counter Increased to : " + counter + "\n"); | |
} | |
else { | |
//System.out.print (" Entering Else block :: \n"); | |
if (counter % 2 == 0){ | |
if (condensedString == "Empty String" ){ | |
condensedString = ""; | |
} | |
condensedString = condensedString + previous; | |
//System.out.print( " --- " + " counter: " + counter + " cndnsdString: " + condensedString + " previous: " + previous +"\n"); | |
} | |
if (i == s.length() - 1 ){ | |
if (condensedString == "Empty String" ){ | |
condensedString = ""; | |
} | |
condensedString = condensedString + current; | |
break; | |
} | |
previous = current; | |
counter = 0; | |
} | |
} | |
//System.out.print( " " + "prevStr: " + previousCondensed + " condensedStr: " + condensedString + "\n"); | |
if( (previousCondensed.equals(condensedString)) || (condensedString == "Empty String") ){ | |
System.out.print(condensedString); | |
return ; | |
} | |
else { | |
previousCondensed = condensedString; | |
stringCondenser(condensedString, previousCondensed); | |
} | |
} | |
public static void main(String[] args) { | |
Scanner input = new Scanner(System.in); | |
String s = input.next(); | |
String previousCondensed = " "; | |
Solution sol = new Solution(); | |
sol.stringCondenser(s, previousCondensed); | |
} | |
} |
This file contains 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.util.*; | |
public class Solution { | |
public static void main(String[] args) { | |
Scanner scan = new Scanner(System.in); | |
String s = scan.next(); | |
scan.close(); | |
while(true){ | |
int len = s.length(); | |
s = s.replaceAll("(.)\\1", ""); | |
if( s.length() == len ){ | |
break; | |
} | |
} | |
System.out.println( (s.isEmpty()) ? "Empty String" : s); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment