Amazon | Virtual Onsite | Score of balanced parenthesis string
Anonymous User
1593

Given a balanced parenthesis string s count the score obtained based on the following rules:

  • (): Score = 1
  • (A): Score = 2 * score of A, where A is a balanced parenthesis string
  • AB: Score = score of A + score of B, where A and B are balanced parenthesis strings

Example:
s is ()(())

Let us first calculate the score for (()). Here the innermost balanced parenthesis string has score of 1 (based on rule 1 above). Thus the whole string has the score 2 * 1 = 2 (based on rule 2)
Now the outer () has a score of 1. Therefore, final score = 1 + 2 = 3 (based on rule 3).

Comments (5)