- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find if an expression has duplicate parenthesis or not in C++
Consider we have an expression exp, and we have to check whether the exp has a duplicate set of parentheses around it or not. An expression will have duplicate parentheses if one sub-expression will be surrounded by more than one parentheses set. For example, if the expression is like −
(5+((7−3)))
Here the sub-expression (7 – 3) is surrounded by two parentheses pair, so these are duplicate parentheses.
To solve this problem, we will use stacks. We will iterate through each character in the exp, and if the character is opening parentheses ‘(’, or any of the operator or operand, then push it into the stack. When the character is closing parentheses, then repeatedly pop characters from the stack until the matching opening parentheses has found, and a counter is used, whose value will be incremented for every character encountered between opening and closing parentheses pair. Which is equal to the value of the counter, is less than 1, then pair of duplicate parentheses is found, otherwise not found.
Example
#include<iostream> #include<stack> using namespace std; bool hasDuplicateParentheses(string str) { stack<char> stk; for (int i = 0; i<str.length(); i++) { char ch = str[i]; if (ch == ')') { char top = stk.top(); stk.pop(); int count = 0; while (top != '(') { count++; top = stk.top(); stk.pop(); } if(count < 1) { return true; } } else stk.push(ch); } return false; } int main() { string str = "(5+((7-3)))"; if (hasDuplicateParentheses(str)) cout << "Duplicate parentheses has Found"; else cout << "No Duplicates parentheses has Found "; }
Output
Duplicate parentheses has Found
- Related Articles
- Check if an Array has fixed size or not in C#
- Check if a Binary Tree (not BST) has duplicate value in C++
- If an expression has no variables, does that mean it's not an equation?
- C# program to find if an array contains duplicate
- Check if an array is synchronized or not in C#
- Check if a binary string has a 0 between 1s or not in C++
- Find if given matrix is Toeplitz or not in C++
- Check if an array is read-only or not in C#
- How can I check if user has an authorization for T-Code or not in SAP system?
- Check if a number is an Unusual Number or not in C++
- Check if a number is an Achilles number or not in C++
- Write a program in Python to check if a series contains duplicate elements or not
- Find if it's possible to rotate the page by an angle or not in C++
- Queries to find whether a number has exactly four distinct factors or not in C++
- C Program to check if an Array is Palindrome or not
