daily pastebin goal
11%
SHARE
TWEET

Untitled

a guest Jun 13th, 2018 54 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public int solution(String s) {
  2.         Stack<Character> stack = new Stack<>();
  3.  
  4.         int count = 0;
  5.  
  6.         Character c;
  7.  
  8.         for(int i = 0; i < s.length(); i++){
  9.             c = s.charAt(i);
  10.  
  11.             if(!stack.empty()) {
  12.                 if(!stack.peek().equals(c)) {
  13.                     stack.pop();
  14.  
  15.                     if(stack.empty())
  16.                         count++;
  17.                 } else {
  18.                     stack.push(c);
  19.                 }
  20.             } else {
  21.                 stack.push(c);
  22.             }
  23.         }
  24.  
  25.         return count;
  26.     }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top