Skip to content

Instantly share code, notes, and snippets.

@kingsamadesu
Created December 3, 2020 20:21
Show Gist options
  • Save kingsamadesu/3a2a64c7a04b2b6be3b4f4ffef05fa60 to your computer and use it in GitHub Desktop.
Save kingsamadesu/3a2a64c7a04b2b6be3b4f4ffef05fa60 to your computer and use it in GitHub Desktop.
856. Score of Parentheses (leetcode).
/*
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