You are viewing a single comment's thread. Return to all comments →
I think that was the most efficient one here. My solution was close, but not that efficient.
static final List<Character> OPENING = Arrays.asList( '{', '[', '(' ), CLOSING = Arrays.asList( '}', ']', ')' ); static final String TRUE = "YES", FALSE = "NO"; static final int NOT_FOUND = -1; static String isBalanced(String s) { Deque<Character> brackets = new ArrayDeque<>(); for ( int i=0; i<s.length(); i++ ) { Character ch = s.charAt(i); int pos = CLOSING.indexOf(ch); if ( pos != NOT_FOUND ) { if ( !OPENING.get(pos).equals( brackets.poll() ) ) return FALSE; } else brackets.push( ch ); } return brackets.isEmpty() ? TRUE : FALSE; }
Seems like cookies are disabled on this browser, please enable them to open this website
Balanced Brackets
You are viewing a single comment's thread. Return to all comments →
I think that was the most efficient one here. My solution was close, but not that efficient.