Skip to content

Akash-nema/N_Queen-problem

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 

Repository files navigation

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;
    
}

}

About

Coding Ninjas Questions solution

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published