Created
August 22, 2021 06:37
-
-
Save emayom/a840bcf1c08a71a0f9a4a94a7d957e87 to your computer and use it in GitHub Desktop.
[프로그래머스] 올바른 괄호
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function solution(s){ | |
s = s.split('()').join(''); | |
const x = s.length; | |
if(s[0] == ")" || s[x-1] == "("){ | |
return false; | |
} else { | |
let cnt = 0; | |
s = s.split(''); | |
for(let i = 0; i < s.length; i++){ | |
if(s[i] == "("){ | |
cnt++; | |
} else if(cnt > 0){ | |
cnt--; | |
} | |
} | |
return (cnt != 0)? false : true; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function solution(s){ | |
s = s.replace(/\(\)/g, ''); | |
const x = s.length; | |
if(s[0] == ")" || s[x-1] == "("){ | |
return false; | |
} else { | |
let cnt = 0; | |
s = s.split(''); | |
for(let i = 0; i < s.length; i++){ | |
if(s[i] == "("){ | |
cnt++; | |
} else if(cnt > 0){ | |
cnt--; | |
} | |
} | |
return (cnt != 0)? false : true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment