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. General PE format layout

  2. Windows Application Data拒绝访问打开方法?

    在Windows7操作系统,打开 Application Data等文件夹时,弹出位置不可用的警告窗口,提示拒绝访问.下面提供简单的解决方法,希望有用. 工具/原料 计算机. Windows7操作系统 ...

  3. mount.nfs: access denied by server while mounting <SERVER IP>:<SERVER PATH>

    root@hipchat:~# mount -t nfs 192.168.10.220:/hipchat/share /home/share/nfs mount.nfs: access denied ...

  4. ./adb: error while loading shared libraries: libncurses.so.5:

    from://http://stackoverflow.com/questions/10005907/eclipse-android-plugin-libncurses-so-5 sudo apt-g ...

  5. 【linux c】setsockopt 详解

    转自:http://blog.csdn.net/zhonglinzhang/article/details/9183229 功能描述:        获取或者设置与某个套接字关联的选 项.选项可能存在 ...

  6. 百度搜索URL参数你知道多少

    http://www.baidu.com/s?wd=关键字 wd(Keyword):查询的关键词: http://www.baidu.com/s?wd=关键字&cl=3 cl(Class):搜 ...

  7. [Android] Implementation vs API dependency

    原文链接: https://jeroenmols.com/blog/2017/06/14/androidstudio3/ https://blog.csdn.net/lonewolf521125/ar ...

  8. [Web 前端] 流行的JavaScript库 ——jQuery

    cp : https://www.cnblogs.com/zhoushihui/p/5141767.html   1.为了简化 JavaScript 的开发, 一些 JavsScript 库诞生了. ...

  9. LRU和LFU的区别

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/guoweimelon/article/details/50855351 一.概念介绍 LRU和LFU ...

  10. CSS 强制换行和禁止换行强制换行 和禁止换行样式

    强制换行 1.word-break: break-all;       只对英文起作用,以字母作为换行依据. 2.word-wrap: break-word;   只对英文起作用,以单词作为换行依据. ...