Created
December 3, 2020 20:21
-
-
Save kingsamadesu/3a2a64c7a04b2b6be3b4f4ffef05fa60 to your computer and use it in GitHub Desktop.
856. Score of Parentheses (leetcode).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Your runtime beats 100.00 % of java submissions. | |
Your memory usage beats 91.19 % of java submissions. | |
*/ | |
class Solution { | |
public int scoreOfParentheses(String S) { | |
int sum = 0 ; | |
int indexStart = 0; | |
int a = 0; | |
for (int i = 0 ; i < S.length() ; i++){ | |
if(S.charAt(i)=='('){ | |
a++; | |
}else{ | |
a--; | |
} | |
if(a==0){ | |
if(i - indexStart == 1){ | |
sum += 1; | |
}else{ | |
sum += 2*scoreOfParentheses(S.substring(indexStart+1,i)); | |
} | |
indexStart=i+1; | |
} | |
} | |
return sum; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment