给定长度为N 的字符串str ,由一对平衡括号组成,任务是根据给定规则计算给定字符串的分数:
- “()”的得分为1 。
- “a b”的分数为a + b ,其中a和b是单独的一对平衡括号。
- “(a)”的分数是a 的两倍,即分数是 a 的2 * 分数。
例子:
Input: str = “()()”
Output: 2
Explanation: The string str is of the form “ab”, that makes the total score = (score of a) + (score of b) = 1 + 1 = 2.
Input: str = “(()(()))”
Output: 6
Explanation: The string str is of the form “(a(b))” which makes the total score = 2 * ((score of a) + 2*(score of b)) = 2*(1 + 2*(1)) = 6.
基于树的方法:有关基于树的方法,请参阅本文的前一篇文章。
时间复杂度: O(N)
辅助空间: O(N)
Stack- based Approach:思路是遍历字符串,在遍历字符串str的同时,如果遇到括号‘)’ ,则计算这对括号的分数。请按照以下步骤解决问题:
- 初始化一个堆栈,比如S ,以跟踪分数并最初将0压入堆栈。
- 使用变量i遍历字符串str并执行以下步骤:
- 如果str[i] 的值等于‘(‘ ,则将0压入堆栈S 。
- 否则,请执行以下步骤:
- 将栈顶S 存储在一个变量中,比如temp ,然后从栈顶弹出元素。
- 如果temp的值非零,则存在内括号。将2 * temp添加到堆栈顶部。否则,将1添加到堆栈顶部。
- 完成上述步骤后,打印栈顶的值作为结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to calculate the score
// of the parentheses using stack
void scoreOfParentheses(string s)
{
// To keep track of the score
stack stack;
// Initially, push 0 to stack
stack.push(0);
// Traverse the string s
for (char c : s) {
// If '(' is encountered,
// then push 0 to stack
if (c == '(')
stack.push(0);
// Otherwise
else {
// Balance the last '(', and store
// the score of inner parentheses
int tmp = stack.top();
stack.pop();
int val = 0;
// If tmp is not zero, it means
// inner parentheses exists
if (tmp > 0)
val = tmp * 2;
// Otherwise, it means no
// inner parentheses exists
else
val = 1;
// Pass the score of this level
// to parent parentheses
stack.top() += val;
}
}
// Print the score
cout << stack.top();
}
// Driver Code
int main()
{
string S = "(()(()))";
scoreOfParentheses(S);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
// Function to calculate the score
// of the parentheses using stack
static void scoreOfParentheses(String s)
{
// To keep track of the score
Stack stack = new Stack<>();
// Initially, push 0 to stack
stack.push(0);
// Traverse the string s
for (char c : s.toCharArray()) {
// If '(' is encountered,
// then push 0 to stack
if (c == '(')
stack.push(0);
// Otherwise
else {
// Balance the last '(', and store
// the score of inner parentheses
int tmp = stack.pop();
int val = 0;
// If tmp is not zero, it means
// inner parentheses exists
if (tmp > 0)
val = tmp * 2;
// Otherwise, it means no
// inner parentheses exists
else
val = 1;
// Pass the score of this level
// to parent parentheses
stack.push(stack.pop() + val);
}
}
// Print the score
System.out.println(stack.peek());
}
// Driver code
public static void main(String[] args)
{
String S = "(()(()))";
// Function call
scoreOfParentheses(S);
}
}
// This code is contributed by Kingash.
Python3
# Python 3 program for the above approach
# Function to calculate the score
# of the parentheses using stack
def scoreOfParentheses(s):
# To keep track of the score
stack = []
# Initially, push 0 to stack
stack.append(0)
# Traverse the string s
for c in s:
# If '(' is encountered,
# then push 0 to stack
if (c == '('):
stack.append(0)
# Otherwise
else:
# Balance the last '(', and store
# the score of inner parentheses
tmp = stack[len(stack) - 1]
stack = stack[:-1]
val = 0
# If tmp is not zero, it means
# inner parentheses exists
if (tmp > 0):
val = tmp * 2
# Otherwise, it means no
# inner parentheses exists
else:
val = 1
# Pass the score of this level
# to parent parentheses
stack[len(stack) - 1] += val
# Print the score
print(stack[len(stack) - 1])
# Driver Code
if __name__ == '__main__':
S = "(()(()))"
scoreOfParentheses(S)
# This code is contributed by bgangwar59.
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
public class GFG
{
// Function to calculate the score
// of the parentheses using stack
static void scoreOfParentheses(String s)
{
// To keep track of the score
Stack stack = new Stack();
// Initially, push 0 to stack
stack.Push(0);
// Traverse the string s
foreach (char c in s.ToCharArray()) {
// If '(' is encountered,
// then push 0 to stack
if (c == '(')
stack.Push(0);
// Otherwise
else {
// Balance the last '(', and store
// the score of inner parentheses
int tmp = stack.Pop();
int val = 0;
// If tmp is not zero, it means
// inner parentheses exists
if (tmp > 0)
val = tmp * 2;
// Otherwise, it means no
// inner parentheses exists
else
val = 1;
// Pass the score of this level
// to parent parentheses
stack.Push(stack.Pop() + val);
}
}
// Print the score
Console.WriteLine(stack.Peek());
}
// Driver code
public static void Main(String[] args)
{
String S = "(()(()))";
// Function call
scoreOfParentheses(S);
}
}
// This code is contributed by 29AjayKumar
输出:
6
时间复杂度: O(N)
辅助空间: O(N)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live