字符串匹配问题
2017-10-26 16:38
176 查看
问题描述:
Given a string containing just the characters ‘(‘, ‘)’, ‘{‘, ‘}’, ‘[’ and ‘]’, determine if the input string is valid.
The brackets must close in the correct order, “()” and “()[]{}” are all valid but “(]” and “([)]” are not.
给定一个字符串,只包含字符“(”、“””、“{”、“}”、“[”和“”),确定输入字符串是否有效。
括号必须以正确的顺序关闭,“()”和“()”{“}”都是有效的,但“()和[([ ] ] ] ]不是。
Given a string containing just the characters ‘(‘, ‘)’, ‘{‘, ‘}’, ‘[’ and ‘]’, determine if the input string is valid.
The brackets must close in the correct order, “()” and “()[]{}” are all valid but “(]” and “([)]” are not.
给定一个字符串,只包含字符“(”、“””、“{”、“}”、“[”和“”),确定输入字符串是否有效。
括号必须以正确的顺序关闭,“()”和“()”{“}”都是有效的,但“()和[([ ] ] ] ]不是。
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> </head> <body> <script type="text/javascript"> var isValid = function isValid(s) { debugger var valid = true, pair = { '(': ')', '[': ']', '{': '}' }, nowWaitingFor = []; for (var i = 0; i < s.length; i++) { var nowChar = s.charAt(i); if (nowChar.match(/[(\[\{]/)) { nowWaitingFor.unshift(pair[nowChar]); } else if (nowChar.match(/[)\]\}]/)) { if (nowWaitingFor[0] !== nowChar) { valid = false; break; } else { nowWaitingFor.splice(0, 1); } } } if (nowWaitingFor.length !== 0) { valid = false; } return valid; }; console.log(isValid("()})")) </script> </body> </html>
相关文章推荐
- 字符串匹配问题
- 字符串匹配问题(int countABC(* s) 输入任何一串字符串,计算机其
- POJ 1159 字符串匹配问题
- 子字符串substring 问题 - KMP 字符串匹配算法备忘录
- 华为编程大赛---含有通配符的字符串匹配问题---基于正则表达式
- 无顺序约束的字符串匹配问题
- 问题 F: 字符串匹配问题
- 字符串匹配问题
- preg_match长字符串匹配失败问题
- ASP VBScript脚本多字符串匹配问题
- LeetCode 44 Wildcard Matching(字符串匹配问题)
- hihocode的map字符串匹配问题
- 常见算法及问题场景——字符串匹配
- 腾讯笔试题之字符串匹配问题
- A - Translation字符串匹配问题
- 字符串匹配问题(栈)
- 玩转经典算法之字符串匹配(一) 问题引入和朴素的匹配算法
- 工作随笔——壹(关于MYSQL数据库中字符串与字符串匹配问题)
- Boyer-Moore(BM)算法,文本查找,字符串匹配问题
- 算法实现字符串匹配问题:从三个字符串中找出共有的最长部分。