Algorithm or program to check for balanced parentheses in an expression using stack data structure.

For example:

[(+) * (-)] // true
{[()]} // true
{(}) // false
{*) //false

The idea to solve the problem is:

  • opening and closing parentheses should have the same number
  • last unclosed should be closed first!
const closing = [')', ']', '}'];
const opening = ['(', '[', '{'];
const matching = {
: ['(', ')'],
: ['[', ']'],
: ['{', '}']
}; const contains = ary => target => ary.includes(target);
const isOpening = contains(opening);
const isClosing = contains(closing); function balanceParentheses (str) {
let stack = [];
const isEmpty = stack => stack.length === ;
const last = stack => stack[stack.length - ]; for (let char of str) {
// if it is open char, push to the stack
if (isOpening(char)) {
stack.push(char);
}
// if it is closing char
else if (isClosing(char)) {
// if stack is not empty
if (!isEmpty(stack)) {
// check last element should be the pair of closing element
const indx = closing.indexOf(char);
const [open, close] = matching[indx];
// if it is, then pop the last element
if (last(stack) === open) {
stack.pop();
} else {
// otherwise, return false
return false;
}
} else {
return false;
}
}
} return isEmpty(stack);
} console.log(balanceParentheses('{[()(){}]}')); // true
console.log(balanceParentheses(')(')); // false
console.log(balanceParentheses('({)}')); // false
console.log(balanceParentheses('{2*3)')); // false
console.log(balanceParentheses('[(1+2)*3-(1-9)]')); //true

[Algorithm] Check for balanced parentheses using stack的更多相关文章

  1. Eclipse下Android开发错误之Unable to execute dex: java.nio.BufferOverflowException. Check the Eclipse log for stack trace

    升级了Android版本后,在运行应用时提示: [2013-11-27 10:37:35 - Dex Loader] Unable to execute dex: java.nio.BufferOve ...

  2. Unable to execute dex: java.nio.BufferOverflowException. Check the Eclipse log for stack trace.

    问题提示:Unable to execute dex: java.nio.BufferOverflowException. Check the Eclipse log for stack trace. ...

  3. [2014-03-13 08:46:42 - Dex Loader] Unable to execute dex: java.nio.BufferOverflowException. Check the Eclipse log for stack trace.

    Unable to execute dex: java.nio.BufferOverflowException. Check the Eclipse log for stack trace. 问题提示 ...

  4. Error处理:Unable to execute dex: java.nio.BufferOverflowException. Check the Eclipse log for stack tra

    [2014-04-20 20:59:23 - MyDetectActivity] Dx  trouble writing output: already prepared [2014-04-20 20 ...

  5. 安卓常见错误Unable to execute dex: java.nio.BufferOverflowException. Check the Eclipse log for stack trace.

    Unable to execute dex: java.nio.BufferOverflowException. Check the Eclipse log for stack trace. 导入新的 ...

  6. leetcode解题报告 32. Longest Valid Parentheses 用stack的解法

    第一道被我AC的hard题!菜鸡难免激动一下,不要鄙视.. Given a string containing just the characters '(' and ')', find the le ...

  7. 32. Longest Valid Parentheses (Stack; DP)

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  8. 20. Valid Parentheses(stack)

    Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...

  9. [Leetcode] 20. Valid Parentheses(Stack)

    括号匹配问题,使用栈的特点,匹配则出栈,否则入栈,最后栈为空则全部匹配.代码如下: class Solution { public: bool isValid(string s) { stack< ...

随机推荐

  1. 孕龙逻辑分析仪 ZeroPlus Logic Analyzer

    Voltage Translation for Analog to Digital Interface ADC http://openschemes.com/2010/03/23/zeroplus-l ...

  2. windows和linux 下将tomcat注册为服务

    参考文献: tomcat注册成windows服务 背景 当前项目需要运行两个Tomcat,每次启动系统以后都要手动进入到tomcat目录执行startup.bat,非常烦,所以想将这两个tomcat直 ...

  3. 防止shell脚本长时间执行导致ssh超时

    在一些对安全性要求较高的场景下.ssh的超时时间是管理员预先设置好的,在闲置一段时间后ssh连接会自己主动断开. 这样的情况下假设通过ssh运行脚本,而脚本运行时间又比較长的话.会导致sshclien ...

  4. DM6467开发领航-开发坏境安装

  5. VB.NET中Module的概念

    今天学习VB.NET,发现VB.NET里面有一个Module的东西,如下图(图-1)所示: 图-1 上网查了一下VB.NET里面的Module,才发现这是学习VB.NET遇到的第一个典型的问题就是:为 ...

  6. mac 刻录ISO系统盘

    今天本本系统坏了,手头上又没有U盘PE工具,只有MAC和光驱,只好在MAC上下载系统ISO刻录,我是直接点ISO文件,右键刻录到光盘,刻录好之后放到本本上发现不能引导,再把光盘放回MAC上一看,光盘里 ...

  7. XE5 修复 安卓 输入法隐藏 后 无法退出的问题 3.1

    (****************************************************)(* *)(* 编写:爱吃猪头肉 & Flying Wang *)(* 上面的版权声 ...

  8. win7设置电脑锁屏时间

    方法/步骤 1 小编用的win7电脑,进入控制面板先~ 2 选择系统与安全选项. 3 如图所示,箭头所指,可以设置锁屏时间,不过电源选项中还有个设置开启屏幕输入密码的设置,第一个就是. 4 如图所示, ...

  9. How to Limit NodeRunner.exe High Memory, CPU Usage

    roblem: NodeRunner.exe is consuming a lot of memory and CPU resulted in performance issues on ShareP ...

  10. [Hook] 免root,自己进程内,startActivity hook的几种姿势

    首先关于startActivity 我们平时会经常使用到 在activity内 直接startActivity(intent) 其实这里还有一种start方式 我们可能没怎么用过 getApplica ...