查找表达式是否有重复的括号
给定一个平衡的表达式,找出它是否包含重复的括号。如果相同的子表达式被多个括号包围,则一组括号是重复的。
例子:
Below expressions have duplicate parenthesis -
((a+b)+((c+d)))
The subexpression "c+d" is surrounded by two
pairs of brackets.
(((a+(b)))+(c+d))
The subexpression "a+(b)" is surrounded by two
pairs of brackets.
(((a+(b))+c+d))
The whole expression is surrounded by two
pairs of brackets.
((a+(b))+(c+d))
(b) and ((a+(b)) is surrounded by two
pairs of brackets.
Below expressions don't have any duplicate parenthesis -
((a+b)+(c+d))
No subsexpression is surrounded by duplicate
brackets.
可以假设给定的表达式是有效的并且不存在任何空格。
这个想法是使用堆栈。遍历给定的表达式,对于表达式中的每个字符,如果该字符是左括号 '(' 或任何运算符或操作数,则将其压入堆栈顶部。如果字符是右括号 ')',然后从堆栈中弹出字符,直到找到匹配的左括号'('并使用一个计数器,其值随着遇到的每个字符递增,直到找到左括号'('。如果在开始和结束之间遇到的字符数括号对,等于计数器的值,小于1,则找到一对重复的括号,否则不会出现多余的括号对。例如,(((a+b))+c) “a+b”周围有重复的括号。当遇到a+b之后的第二个“)”时,堆栈包含“((”。由于堆栈的顶部是一个左括号,因此可以断定存在重复的括号.
以下是上述想法的实现:
C++
// C++ program to find duplicate parenthesis in a
// balanced expression
#include
using namespace std;
// Function to find duplicate parenthesis in a
// balanced expression
bool findDuplicateparenthesis(string str)
{
// create a stack of characters
stack Stack;
// Iterate through the given expression
for (char ch : str)
{
// if current character is close parenthesis ')'
if (ch == ')')
{
// pop character from the stack
char top = Stack.top();
Stack.pop();
// stores the number of characters between a
// closing and opening parenthesis
// if this count is less than or equal to 1
// then the brackets are redundant else not
int elementsInside = 0;
while (top != '(')
{
elementsInside++;
top = Stack.top();
Stack.pop();
}
if(elementsInside < 1) {
return 1;
}
}
// push open parenthesis '(', operators and
// operands to stack
else
Stack.push(ch);
}
// No duplicates found
return false;
}
// Driver code
int main()
{
// input balanced expression
string str = "(((a+(b))+(c+d)))";
if (findDuplicateparenthesis(str))
cout << "Duplicate Found ";
else
cout << "No Duplicates Found ";
return 0;
}
Java
import java.util.Stack;
// Java program to find duplicate parenthesis in a
// balanced expression
public class GFG {
// Function to find duplicate parenthesis in a
// balanced expression
static boolean findDuplicateparenthesis(String s) {
// create a stack of characters
Stack Stack = new Stack<>();
// Iterate through the given expression
char[] str = s.toCharArray();
for (char ch : str) {
// if current character is close parenthesis ')'
if (ch == ')') {
// pop character from the stack
char top = Stack.peek();
Stack.pop();
// stores the number of characters between a
// closing and opening parenthesis
// if this count is less than or equal to 1
// then the brackets are redundant else not
int elementsInside = 0;
while (top != '(') {
elementsInside++;
top = Stack.peek();
Stack.pop();
}
if (elementsInside < 1) {
return true;
}
} // push open parenthesis '(', operators and
// operands to stack
else {
Stack.push(ch);
}
}
// No duplicates found
return false;
}
// Driver code
public static void main(String[] args) {
// input balanced expression
String str = "(((a+(b))+(c+d)))";
if (findDuplicateparenthesis(str)) {
System.out.println("Duplicate Found ");
} else {
System.out.println("No Duplicates Found ");
}
}
}
Python3
# Python3 program to find duplicate
# parenthesis in a balanced expression
# Function to find duplicate parenthesis
# in a balanced expression
def findDuplicateparenthesis(string):
# create a stack of characters
Stack = []
# Iterate through the given expression
for ch in string:
# if current character is
# close parenthesis ')'
if ch == ')':
# pop character from the stack
top = Stack.pop()
# stores the number of characters between
# a closing and opening parenthesis
# if this count is less than or equal to 1
# then the brackets are redundant else not
elementsInside = 0
while top != '(':
elementsInside += 1
top = Stack.pop()
if elementsInside < 1:
return True
# push open parenthesis '(', operators
# and operands to stack
else:
Stack.append(ch)
# No duplicates found
return False
# Driver Code
if __name__ == "__main__":
# input balanced expression
string = "(((a+(b))+(c+d)))"
if findDuplicateparenthesis(string) == True:
print("Duplicate Found")
else:
print("No Duplicates Found")
# This code is contributed by Rituraj Jain
C#
// C# program to find duplicate parenthesis
// in a balanced expression
using System;
using System.Collections.Generic;
class GFG
{
// Function to find duplicate parenthesis
// in a balanced expression
static Boolean findDuplicateparenthesis(String s)
{
// create a stack of characters
Stack Stack = new Stack();
// Iterate through the given expression
char[] str = s.ToCharArray();
foreach (char ch in str)
{
// if current character is
// close parenthesis ')'
if (ch == ')')
{
// pop character from the stack
char top = Stack.Peek();
Stack.Pop();
// stores the number of characters between
// a closing and opening parenthesis
// if this count is less than or equal to 1
// then the brackets are redundant else not
int elementsInside = 0;
while (top != '(')
{
elementsInside++;
top = Stack.Peek();
Stack.Pop();
}
if (elementsInside < 1)
{
return true;
}
}
// push open parenthesis '(',
// operators and operands to stack
else
{
Stack.Push(ch);
}
}
// No duplicates found
return false;
}
// Driver code
public static void Main(String[] args)
{
// input balanced expression
String str = "(((a+(b))+(c+d)))";
if (findDuplicateparenthesis(str))
{
Console.WriteLine("Duplicate Found ");
}
else
{
Console.WriteLine("No Duplicates Found ");
}
}
}
// This code is contributed by 29AjayKumar
Javascript
输出:
Duplicate Found
上述解决方案的时间复杂度为 O(n)。
程序使用的辅助空间为 O(n)。