Codility - Brackets - Problem and Solution  

by ne on 2022-02-16 under Algo/DS/Problems

Hi,
The problem description is in the comments above the solution below.

The problem belongs to Codility, you can try out it yourself on their platform.
 


import java.util.*;

// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");

public class Brackets {
    public int solution(String S) {
        if(S.length() == 0) return 1;
        Stack st = new Stack<>();
        for(int i=0;i 0) {
                    if (st.peek() == '(' && c == ')') {
                        st.pop();
                    } else if (st.peek() == '[' && c == ']') {
                        st.pop();
                    } else if (st.peek() == '{' && c == '}') {
                        st.pop();
                    } else{
                        return 0;
                    }
                } else {
                    return 0;
                }
            }
            //System.out.println(Arrays.toString(st.toArray()));
        }

        return st.size() > 0 ? 0:1;
    }
}