開カッコと閉カッコからなる文字列が与えられます。 開カッコと閉カッコがきちんと対応しているかを判定してください。 カッコが対応しているとは以下を満たすことを言います。 - 任意の開カッコに対して、*それより後に*ペアになる閉カッコが存在すること - ペアになっていないカッコが存在しないこと ``` # 入力と出力の例 () => True )( => False # 閉カッコが先に来てしまっている ( => False ) => False ()() => True (()) => True ()) => False (()(())) => True ``` ```:typescript function checkBraces(input: string): boolean { if (input % 2 === 1) { return false; } const startCount = input.match(/\(/g).length; const endCount = input.match(/\)/g).length; if (startCount !== endCount) { return false; } let tmpStr = input; while(tmpStr !== '') { if (tmpStr.startsWith(')')) { break; } tmpStr = tmpStr.replace('()', ''); } if (tmpStr !== '') { return false; } return true; // (()(())) // ((())) // (()) // () // () } ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up