You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+45Lines changed: 45 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1288,3 +1288,48 @@ const songDecoder = song => {
1288
1288
---
1289
1289
1290
1290
**[⬆ 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
+
constisValid=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
+
constisValid=s=> {
1313
+
conststack= [];
1314
+
constpairs= {
1315
+
'(':')',
1316
+
'[':']',
1317
+
'{':'}',
1318
+
};
1319
+
1320
+
for (constcharof s) {
1321
+
if (pairs[char]) {
1322
+
stack.push(char);
1323
+
} elseif (pairs[stack.pop()] !== char) {
1324
+
returnfalse;
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