[Algorithm] Check for balanced parentheses using stack
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的更多相关文章
- 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 ...
- 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. ...
- [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. 问题提示 ...
- 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 ...
- 安卓常见错误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. 导入新的 ...
- leetcode解题报告 32. Longest Valid Parentheses 用stack的解法
第一道被我AC的hard题!菜鸡难免激动一下,不要鄙视.. Given a string containing just the characters '(' and ')', find the le ...
- 32. Longest Valid Parentheses (Stack; DP)
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- 20. Valid Parentheses(stack)
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- [Leetcode] 20. Valid Parentheses(Stack)
括号匹配问题,使用栈的特点,匹配则出栈,否则入栈,最后栈为空则全部匹配.代码如下: class Solution { public: bool isValid(string s) { stack< ...
随机推荐
- kettle的基本介绍
Kettle 主要内容: 一.ETL介绍 二.Kettle介绍 三.Java调用Kettle API 一.ETL介绍 1. ETL是什么? 1).ETL分别是“Extract”.“ Transform ...
- [Go] 反射 - reflect.ValueOf()
类型 和 接口 由于反射是基于类型系统(type system)的,所以先简单了解一下类型系统. 首先 Golang 是一种静态类型的语言,在编译时每一个变量都有一个类型对应,例如:int, floa ...
- 正确识别希捷Backup Plus新睿品1TB正品,杜绝奸商猖獗
刚刚在百度希捷贴吧发了此贴, 马上被删除, 无奈只能发于个人博客, 望看到的朋友能转载到"合适"位置,让更多的朋友看到. 避免上当. 最近准备买个移动硬盘备份电脑资料,看上了睿品 ...
- ASP.NET MVC异步验证是如何工作的01,jQuery的验证方式、错误信息提示、validate方法的背后
ASP.NET MVC借助jQuery的验证机制,提供了一套从客户端到服务端异步验证的解决方案,通常情况下,用起来相当方便.但面对一些相对特殊的情况,可能会遇到验证失效的场景,比如在使用ajax动态异 ...
- 【jvm】java查看内存使用jmap,jstat和jstack使用 ,docker启动服务下查看jvm使用情况
[声明,如果是docker启动的服务,可以在进入容器内后,再使用如下命令操作] [docker exec -it 容器ID /bin/bash 即可进入容器内] [如果不是docker启动的 ...
- C/C++中const关键字
http://blog.csdn.net/xdrt81y/article/details/24333335 今天在做一个趋势笔试题的时候,才让我有了系统把const关键字好好总结一下的冲动,因为这个关 ...
- 架构:The Onion Architecture : part 2(洋葱架构:第二篇)(转载)
原位地址:http://jeffreypalermo.com/blog/the-onion-architecture-part-2/. In part 1, I introduced an archi ...
- Java移位运算符详解实例——左移位运算符>>、带符号的右移位运算符>>
移位运算符也针对二进制的“位”,它主要包括:左移位运算符(<<).右移位运算符(>>>).带符号的右移位运算符(>>). 1.左移运算符左移运算符用“< ...
- Java-----隐藏手机号中间四位,身份证号码中间几位
phone.replaceAll("(\\d{3})\\d{4}(\\d{4})","$1****$2");152****4799 idCard.replace ...
- 《学习R》
<学习R> 基本信息 原书名:Learning R 原出版社: O'Reilly Media 作者: (美)Richard Cotton 译者: 刘军 丛书名: 图灵程序设计丛书 出版社: ...