FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
JavaScript-Interview-Question/ValidParentheses.js at master · shaantanu9/JavaScript-Interview-Question · GitHub
shaantanu9
/
JavaScript-Interview-Question
Public
forked from
namitmalasi/JavaScript-Interview-Question
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
JavaScript-Interview-Question
/
ValidParentheses.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
34 lines (28 loc) · 871 Bytes
Breadcrumbs
JavaScript-Interview-Question
/
ValidParentheses.js
Copy path
File metadata and controls
34 lines (28 loc) · 871 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/*
Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
An input string is valid if:
Open brackets must be closed by the same type of brackets.
Open brackets must be closed in the correct order.
*/
const
isValidParentheses
=
function
(
s
)
{
const
map
=
{
"("
:
")"
,
"{"
:
"}"
,
"["
:
"]"
,
}
;
let
stack
=
[
]
;
for
(
let
i
=
0
;
i
<
s
.
length
;
i
++
)
{
if
(
s
[
i
]
===
"("
||
s
[
i
]
===
"{"
||
s
[
i
]
===
"["
)
{
stack
.
push
(
s
[
i
]
)
;
}
else
{
if
(
map
[
stack
.
pop
(
)
]
!==
s
[
i
]
)
return
false
;
}
}
return
stack
.
length
?
false
:
true
;
}
;
console
.
log
(
isValidParentheses
(
"()"
)
)
;
console
.
log
(
isValidParentheses
(
"(){}[]"
)
)
;
console
.
log
(
isValidParentheses
(
"(]"
)
)
;
console
.
log
(
isValidParentheses
(
"([)]"
)
)
;
console
.
log
(
isValidParentheses
(
"{[]}"
)
)
;
console
.
log
(
isValidParentheses
(
"(("
)
)
;
Back
|
FazBrowse Home
|
New Git URL