Balanced Parentheses (Recursive)


Submit solution

Points: 50
Time limit: 1.0s
Memory limit: 10M

Authors:
Problem type
Allowed languages
Java 19

Description

給定一個只包含 () 的字串,請使用 遞迴 function 判斷此字串是否為平衡括號字串。

平衡括號字串的定義如下:

  1. 空字串是平衡的
  2. A 是平衡字串,則 (A) 也是平衡字串
  3. AB 都是平衡字串,則 AB 也是平衡字串

請設計一個程式,讀入一個字串,並輸出:

  • Balanced:若字串平衡
  • Not Balanced:若字串不平衡

Requirements

  1. 必須使用遞迴 method 完成,不可只用迴圈直接判斷
  2. 可自行設計 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

There are no comments at the moment.