korobushk

Stackremoveconsecutives

Apr 17th, 2021
844
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.   public static void main(String[] args) {
  2.         String a = "aaaaaabaabccccccc";
  3.         System.out.println(removeConsecutiveDuplicates(a));
  4.     }
  5.  
  6.     public static String removeConsecutiveDuplicates(String str) {
  7.         // Your code here
  8.         Stack<Character> stack = new Stack<>();
  9.         String output = "";
  10.  
  11.         for (int i = 0; i < str.length(); i++) {
  12.             if (stack.isEmpty()||str.charAt(i) != stack.peek()) {
  13.                 stack.push(str.charAt(i));
  14.                 output+=str.charAt(i);
  15.             }
  16.         }
  17.  
  18.         return output;
  19.     }
  20. }
  21.  
  22. //OUTPUT
  23. //ababc
  24.  
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×