Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void main(String[] args) {
- String a = "aaaaaabaabccccccc";
- System.out.println(removeConsecutiveDuplicates(a));
- }
- public static String removeConsecutiveDuplicates(String str) {
- // Your code here
- Stack<Character> stack = new Stack<>();
- String output = "";
- for (int i = 0; i < str.length(); i++) {
- if (stack.isEmpty()||str.charAt(i) != stack.peek()) {
- stack.push(str.charAt(i));
- output+=str.charAt(i);
- }
- }
- return output;
- }
- }
- //OUTPUT
- //ababc
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement