開カッコと閉カッコからなる文字列が与えられます。 開カッコと閉カッコがきちんと対応しているかを判定してください。 カッコが対応しているとは以下を満たすことを言います。 - 任意の開カッコに対して、*それより後に*ペアになる閉カッコが存在すること - ペアになっていないカッコが存在しないこと ``` # 入力と出力の例 () => True )( => False # 閉カッコが先に来てしまっている ( => False ) => False ()() => True (()) => True ()) => False (()(())) => True (()))(=> False (()))((())=> False ``` 言語はなんでも大丈夫です! ```typescript= const checkBraces = (input: string): boolean => { const splittedInput = input.split(""); let result: boolean = false; let unMatchedBraces: string[] = []; if ( splittedInput[0] === ")" || splittedInput[splittedInput.length - 1] === "(" ) { result = false; } else { splittedInput.forEach((item) => { unMatchedBraces.push(item); const lastIndex = unMatchedBraces.length - 1; if ( unMatchedBraces[lastIndex - 1] === "(" && unMatchedBraces[lastIndex] === ")" ) { unMatchedBraces.splice(lastIndex - 1, 2); } }); result = unMatchedBraces.length === 0 ? true : false; } return result; }; ```
×
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