c++ and java solutions of bracket counting