文字列マッチング問題
3725 ワード
問題の説明:Ginven a string containing just the characters'('、')'''、''''''''、''''and'''で、determine if the input string is valid.
The braackets must close in the corectorder,"()""are all valid but"(")"(")""are"(")""are"([]]]]are not.文字列を指定します.文字列は、文字("、"""""""""、"""")、",",",",","",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",","すべて有効ですが、「()」と「()」は違います.
The braackets must close in the corectorder,"()""are all valid but"(")"(")""are"(")""are"([]]]]are not.文字列を指定します.文字列は、文字("、"""""""""、"""")、",",",",","",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",","すべて有効ですが、「()」と「()」は違います.
<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>