Last active
April 26, 2020 16:23
-
-
Save zaidw21/2af65170f5c3a96763a447842609e3b8 to your computer and use it in GitHub Desktop.
Test
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
static class Entity { | |
char character; | |
int frequency; | |
Entity(char p, int q) { | |
character = p; | |
frequency = q; | |
} | |
} | |
List<String> list = new ArrayList<>(); | |
Stack<Entity> st = new Stack<>(); | |
int i = 0; | |
for (i = 0; i < inputArray.size(); i++) { | |
char x = inputArray.get(i).charAt(0); | |
if (!st.isEmpty() && st.peek().frequency == burstLength) { | |
char curr = st.peek().character; | |
while (!st.isEmpty() && st.peek().character == curr) | |
st.pop(); | |
} | |
if (!st.isEmpty() && st.peek().character == x) { | |
Entity newTop = new Entity(x, st.peek().frequency + 1); | |
st.push(newTop); | |
} | |
else { | |
Entity newTop = new Entity(x, 1); | |
st.push(newTop); | |
} | |
} | |
if (!st.isEmpty() && st.peek().frequency == burstLength) { | |
char curr = st.peek().character; | |
while (!st.isEmpty() && st.peek().character == curr) | |
st.pop(); | |
} | |
while (!st.isEmpty()) | |
list.add(Character.toString(st.pop().character)); | |
Collections.reverse(list); | |
return list; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
static class Entity {
char character;
int frequency;