開カッコと閉カッコからなる文字列が与えられます。
開カッコと閉カッコがきちんと対応しているかを判定してください。
カッコが対応しているとは以下を満たすことを言います。
- 任意の開カッコに対して、*それより後に*ペアになる閉カッコが存在すること
- ペアになっていないカッコが存在しないこと
You are given a string consisting of opening and closing brackets.
Determine whether the opening and closing brackets correspond properly.
Corresponding brackets are defined as follows
- For any given open parenthesis, there must be a pair of closing parentheses *after* it.
- There are no unpaired parentheses.
```
# 入力と出力の例
() => True
)( => False # 閉カッコが先に来てしまっている
( => False
) => False
()() => True
(()) => True
()) => False
(()(())) => True
```
```:typescript
// stack
// ( => push
// ) => pop
function checkBraces(input: string): boolean {
const sum = 0;
const chars = Array.from(input);
for (const char of chars) {
if(sum < 0) {
break;
}
if(char === '(') {
sum += 1;
}
if(char === ')') {
sum -= 1
}
}
return sum == 0;
}
```