检查表达式是否包含多余的括号 |设置 2
给定字符串平衡表达式,找出它是否包含多余的括号。如果相同的子表达式被不必要的或多个括号包围,则一组括号是多余的。如果多余则打印“是”,否则打印“否”。
注意:表达式可能包含'+' 、 '*' 、 '–'和'/'运算符。给定的表达式是有效的,并且不存在空格。
注意:该问题旨在解决 O(1) 额外空间。
例子:
Input: ((a+b))
Output: YES
((a+b)) can reduced to (a+b)
Input: (a+(b)/c)
Output: YES
(a+(b)/c) can reduced to (a+b/c) because b is surrounded by () which is redundant
Input: (a+b*(c-d))
Output: NO
(a+b*(c-d)) doesn’t have any redundant or multiple brackets
方法:
这个想法与上一篇文章中讨论的想法非常相似,但在这里我们计算符号( '+' 、 '*' 、 '-'和'/' )和括号中使用的总数表达。
如果括号的数量不等于符号的数量,则该函数将返回 false。
C++
// C++ program to check for/
// redundant braces in the string
#include
using namespace std;
// Function to check for
// redundant braces
bool IsRedundantBraces(string A)
{
// count of no of signs
int a = 0, b = 0;
for (int i = 0; i < A.size(); i++) {
if (A[i] == '('
&& A[i + 2] == ')')
return 1;
if (A[i] == '*'
|| A[i] == '+'
|| A[i] == '-'
|| A[i] == '/')
a++;
if (A[i] == '(')
b++;
}
if (b > a)
return 1;
return 0;
}
// Driver function
int main()
{
string A = "(((a+b) + c) + d)";
if (IsRedundantBraces(A)) {
cout << "YES\n";
}
else {
cout << "NO";
}
}
Java
// Java program to check for
// redundant braces in the string
class GFG
{
// Function to check for
// redundant braces
static boolean IsRedundantBraces(String A)
{
// count of no of signs
int a = 0, b = 0;
for (int i = 0; i < A.length(); i++)
{
if (A.charAt(i) == '(' &&
A.charAt(i + 2) == ')')
return true;
if (A.charAt(i) == '*' ||
A.charAt(i) == '+' ||
A.charAt(i) == '-' ||
A.charAt(i) == '/')
a++;
if (A.charAt(i) == '(')
b++;
}
if (b > a)
return true;
return false;
}
// Driver Code
public static void main (String[] args)
{
String A = "(((a+b) + c) + d)";
if (IsRedundantBraces(A))
{
System.out.println("YES");
}
else
{
System.out.println("NO");
}
}
}
// This code is contributed by AnkitRai01
Python3
# Python3 program to check for/
# redundant braces in the string
# Function to check for
# redundant braces
def IsRedundantBraces(A):
# count of no of signs
a, b = 0, 0;
for i in range(len(A)):
if (A[i] == '(' and A[i + 2] == ')'):
return True;
if (A[i] == '*' or A[i] == '+' or
A[i] == '-' or A[i] == '/'):
a += 1;
if (A[i] == '('):
b += 1;
if (b > a):
return True;
return False;
# Driver Code
if __name__ == '__main__':
A = "(((a+b) + c) + d)";
if (IsRedundantBraces(A)):
print("YES");
else:
print("NO");
# This code is contributed by PrinciRaj1992
C#
// C# program to check for
// redundant braces in the string
using System;
class GFG
{
// Function to check for
// redundant braces
static bool IsRedundantBraces(string A)
{
// count of no of signs
int a = 0, b = 0;
for (int i = 0; i < A.Length; i++)
{
if (A[i] == '(' && A[i + 2] == ')')
return true;
if (A[i] == '*' || A[i] == '+' ||
A[i] == '-' || A[i] == '/')
a++;
if (A[i] == '(')
b++;
}
if (b > a)
return true;
return false;
}
// Driver Code
public static void Main (String[] args)
{
String A = "(((a+b) + c) + d)";
if (IsRedundantBraces(A))
{
Console.WriteLine("YES");
}
else
{
Console.WriteLine("NO");
}
}
}
// This code is contributed by PrinciRaj1992
Javascript
输出:
NO