Here you can find the source of removeBackets(String cont)
public static String removeBackets(String cont)
//package com.java2s; import java.util.Stack; public class Main { public static String removeBackets(String cont) { if (cont == null) return null; int len = cont.length(); Stack<Character> stack = new Stack<Character>(); char c;/*from w w w . j av a 2 s. c o m*/ for (int i = 0; i < len; i++) { c = cont.charAt(i); if (c == ')') { while (true) { c = (Character) stack.pop(); if (c == '(') { break; } } } else { stack.push(c); } } StringBuilder sb = new StringBuilder(); while (stack.isEmpty() == false) { c = (Character) stack.pop(); sb.append(c); } return sb.reverse().toString(); } }