Given a string s consisting of parentheses, you need to find the maximum score possible in a balanced substring of s. The score of a substring is calculated by choosing two indices i and j (0 <= i < j < len(s)) such that s[i] is an opening parenthesis "(" and s[j] is a closing parenthesis ")". The score of the substring is defined as j - i, i.e., the difference between the indices.
Write a function/method that takes a string s as input and returns the maximum score that can be obtained from a balanced substring of s.
Note
The input string s will only consist of opening and closing parentheses. A balanced substring is a substring that has an equal number of opening and closing parentheses.
Examples
01 Β· Example 1
s = "(())" return = 4
There are two possible balanced substrings: "( ( ) )" (3-0) + (2-1) = 4 or (2-0) + (3-1) = 4
02 Β· Example 2
s = "()()" return = 3
We dont need to use the two parenthesis in the middle :)
Updated on 02-04-2025, relative image will be uploaded next time :P
Constraints
N/A (feel free to contact us if you know about it. TYSM!More Amazon problems
- Count Promotional PeriodsOA Β· Seen Jun 2026
- Find Maximum Total Amount (SDE I, Fungible :)Seen Jun 2026
- Get Minimum AmountOA Β· Seen Jun 2026
- Find Minimum CostOA Β· Seen Jun 2026
- Get Smallest Base SegmentOA Β· Seen Jun 2026
- Select Least Resource TasksOA Β· Seen Jun 2026
- Product Category Group SizesPHONE SCREEN Β· Seen May 2026
- Count Connected ComponentsPHONE SCREEN Β· Seen May 2026
public int maximumScoreInBalancedString(String s) {
// write your code here
}
s"(())"
expected4
sign in to submit