2022-03-17 Daily-Challenge
Today I have done leetcode's March LeetCoding Challenge with cpp
.
March LeetCoding Challenge 17
Description
Score of Parentheses
iven a balanced parentheses string s
, return the score of the string.
The score of a balanced parentheses string is based on the following rule:
"()"
has score1
.AB
has scoreA + B
, whereA
andB
are balanced parentheses strings.(A)
has score2 * A
, whereA
is a balanced parentheses string.
Example 1:
Input: s = "()"
Output: 1
Example 2:
Input: s = "(())"
Output: 2
Example 3:
Input: s = "()()"
Output: 2
Constraints:
2 <= s.length <= 50
s
consists of only'('
and')'
.
Solution
class Solution {
public:
int scoreOfParentheses(string S) {
int answer = 0;
int count = 0;
char last = 0;
for(auto c : S) {
if(c == '(') {
count += 1;
} else {
count -= 1;
if(last == '(') answer += (1 << count);
}
last = c;
}
return answer;
}
};
// Accepted
// 86/86 cases passed (0 ms)
// Your runtime beats 100 % of cpp submissions
// Your memory usage beats 99.93 % of cpp submissions (5.8 MB)