Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8058956

Browse files
committed
Add a question
1 parent 805c1cd commit 8058956

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

‎README.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1288,3 +1288,48 @@ const songDecoder = song => {
12881288
---
12891289

12901290
**[⬆ Back to Top](#javascript-coding-challenges-for-beginners)**
1291+
1292+
## 40. Valid Parentheses
1293+
1294+
Given a non-empty string `s` containing just the characters `(`, `)`, `{`, `}`, `[` and `]`, determine if the input string is valid. An input string is valid if open brackets are closed by the same type of brackets, and open brackets are closed in the correct order.
1295+
1296+
```js
1297+
const isValid = s => {
1298+
// Your solution
1299+
};
1300+
1301+
console.log(isValid('[')); //false
1302+
console.log(isValid('()')); //true
1303+
console.log(isValid('(]')); //false
1304+
console.log(isValid('{[]}')); //true
1305+
console.log(isValid('([)]')); //false
1306+
console.log(isValid('()[]{}')); //true
1307+
```
1308+
1309+
<details><summary>Solution</summary>
1310+
1311+
```js
1312+
const isValid = s => {
1313+
const stack = [];
1314+
const pairs = {
1315+
'(': ')',
1316+
'[': ']',
1317+
'{': '}',
1318+
};
1319+
1320+
for (const char of s) {
1321+
if (pairs[char]) {
1322+
stack.push(char);
1323+
} else if (pairs[stack.pop()] !== char) {
1324+
return false;
1325+
}
1326+
}
1327+
return !stack.length;
1328+
};
1329+
```
1330+
1331+
</details>
1332+
1333+
---
1334+
1335+
**[⬆ Back to Top](#javascript-coding-challenges-for-beginners)**

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /