Balanced Parentheses (Recursive)
Submit solution
Java 19
Points:
50
Time limit:
1.0s
Memory limit:
10M
Authors:
Problem type
Allowed languages
Description
給定一個只包含 ( 與 ) 的字串,請使用 遞迴 function 判斷此字串是否為平衡括號字串。
平衡括號字串的定義如下:
- 空字串是平衡的
- 若
A是平衡字串,則(A)也是平衡字串 - 若
A與B都是平衡字串,則AB也是平衡字串
請設計一個程式,讀入一個字串,並輸出:
Balanced:若字串平衡Not Balanced:若字串不平衡
Requirements
- 必須使用遞迴 method 完成,不可只用迴圈直接判斷
- 可自行設計 method,例如:
isBalanced(String s)check(String s, int start, int end)
Input / Output
| Sample 1 | Sample 2 | Sample 3 | |
|---|---|---|---|
| Input | ()() | (()()) | (() |
| Output | Balanced | Balanced | Not Balanced |
Comments