-
Notifications
You must be signed in to change notification settings - Fork 694
/
Copy pathSolution.java
69 lines (53 loc) · 2.03 KB
/
Solution.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//Problem: https://www.hackerrank.com/challenges/reduced-string
//Java 8
/*
We can simply compress by iterating left to right removing duplicates
then repeat the process until we can no longer compress
To accomplish this we just need to loop until we see no change
in our input and output after running the compression algorithm
*/
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
Scanner input = new Scanner(System.in);
String inputString = input.nextLine();
StringBuilder lastOutput = new StringBuilder(inputString);
while(true)
{
StringBuilder currentOutput = new StringBuilder("");
String s = lastOutput.toString();
char past = s.charAt(0);
int count = 0;
for(int i = 0; i < s.length(); i++)
{
char current = s.charAt(i);
if(past == current)
count += 1;
else if (count == 1)
{
currentOutput.append(past);
count = 1;
}
else
count = 1;
if(count == 2)
count = 0;
past = current;
}
if(count == 1)
currentOutput.append(s.charAt(s.length()-1));
if(currentOutput.toString().equals(""))
{
System.out.println("Empty String");
System.exit(0);
}
if(currentOutput.toString().equals(lastOutput.toString()))
break;
else
lastOutput = new StringBuilder(currentOutput.toString());
}
System.out.println(lastOutput);
}
}