Skip to content

Latest commit

 

History

History
39 lines (36 loc) · 1.01 KB

README.md

File metadata and controls

39 lines (36 loc) · 1.01 KB

import java.util.Stack; public class Solution {

public static int countBracketReversals(String input) {
	//Your code goes here
    if(input.length()%2==1){
        return -1;
    }
    Stack<Character> stack=new Stack<Character>();
    
    for(int i=0;i<input.length();i++){
        if(input.charAt(i)=='{'){
            stack.push(input.charAt(i));
        }else{
            if(stack.isEmpty()){
                stack.push(input.charAt(i));
            }else{
                if(stack.peek()=='{'){
                    stack.pop();
                }else{
                    stack.push(input.charAt(i));
                }
            }         
        }
    }
     int count=0;
    while(stack.isEmpty()!=true){
        char c1=stack.pop();
        char c2=stack.pop();
        if(c1==c2){
            count++;
        }else{
            count=count+2;
        }
    }
    return count;
    
}

}