Facebook面试题 Remove unclosed parenthesis
2017-01-10 07:01
411 查看
Given a String with parentheses, return a string with balanced parentheses by removing fewest characters possible. You cannot add anything to the string.
E.g.
balabce(“()”) -> “()”
balance(“)(“) -> “”
balance(“(((((()”) -> “()”
balance(“()(((()()”) -> “()()()”
Analysis:
Obviously the requirement of this question is to delete all unclosed parentheses and reserve closed parentheses. At the same time, we need to close a right parenthesis with its nearest left parenthesis.
The idea is like following:
Traverse the string from left to right. Count the number of left and right parenthesis. If the number of right parenthesis is greater than the number of left parenthesis, delete the right parenthesis.
Traverse the string from right to left. Count the number of left and right parenthesis. If the number of left parenthesis is greater than the number of right parenthesis, delete the left parenthesis.
Java implementation:
Github链接
E.g.
balabce(“()”) -> “()”
balance(“)(“) -> “”
balance(“(((((()”) -> “()”
balance(“()(((()()”) -> “()()()”
Analysis:
Obviously the requirement of this question is to delete all unclosed parentheses and reserve closed parentheses. At the same time, we need to close a right parenthesis with its nearest left parenthesis.
The idea is like following:
Traverse the string from left to right. Count the number of left and right parenthesis. If the number of right parenthesis is greater than the number of left parenthesis, delete the right parenthesis.
Traverse the string from right to left. Count the number of left and right parenthesis. If the number of left parenthesis is greater than the number of right parenthesis, delete the left parenthesis.
O(2n) = O(n)time,
O(1)space. n is the length of the string.
Java implementation:
public class Solution { public String balance(String s) { int l = s.length(); char[] chs = s.toCharArray(); int L = 0; int R = 0; for (int i = 0; i < l; i++) { if (chs[i] == '(') L++; if (chs[i] == ')') R++; if (R > L) { chs[i] = '*'; R--; } } L = 0; R = 0; for (int i = l - 1; i >= 0; i--) { if (chs[i] == ')') R++; if (chs[i] == '(') L++; if (L > R) { chs[i] = '*'; L--; } } StringBuilder sb = new StringBuilder(); for (int i = 0; i < l; i++) { if (chs[i] != '*') { sb.append(String.valueOf(chs[i])); } } return sb.toString(); } }
Github链接
相关文章推荐
- Facebook面试题 Remove duplicates from array where all duplicate numbers are located adjacently
- LeetCode 301: Remove Invalid Parenthesis
- 16 多校8 Rikka with Parenthesis II
- HDU 5831 Rikka with Parenthesis II (贪心)
- 九章算法 | Facebook 面试题 : 岛的周长
- Generate parenthesis
- sre_constants.error: unbalanced parenthesis
- Facebook面试题 Moving positive, negative and zeros in an array
- HDU 5831 Rikka with Parenthesis II(栈的使用)
- hdu 5831 Rikka with Parenthesis II 2016 Multi-University 8
- Oracle 11g: ORA-00907: missing right parenthesis solution
- Careercup - Facebook面试题 - 5435439490007040
- 2016年湖南省第十二届大学生计算机程序设计竞赛---Parenthesis(线段树求区间最值)
- HDU 5831 Rikka with Parenthesis II
- CSU 1809 Parenthesis 【前缀和+RMQ】
- HHUOJ 1023 Mouse and Parenthesis(线段树)
- Oracle报错:missing right parenthesis什么问题?
- MYSQL数据迁移tips,ORA-00907: missing right parenthesis
- error C2338: THE_BRACKET_OPERATOR_IS_ONLY_FOR_VECTORS__USE_THE_PARENTHESIS_OPERATOR_INSTEAD
- CSU 1809 Parenthesis(RMQ||线段树)