Given an expression, find and mark matched and unmatched parenthesis in it. We need to replace all balanced opening parenthesis with 0, balanced closing parenthesis with 1, and all unbalanced with -1.
Examples:
Input : ((a)
Output : -10a1
Input : (a))
Output : 0a1-1
Input : (((abc))((d)))))
Output : 000abc1100d111-1-1
The idea is based on a stack. We run a loop from the start of the string Up to end and for every ‘(‘, we push it into a stack. If the stack is empty, and we encounter a closing bracket ‘)’ we replace -1 at that index of the string. Else we replace all opening brackets ‘(‘ with 0 and closing brackets with 1. Then pop from the stack.
C++
#include <bits/stdc++.h>
using namespace std;
void identifyParenthesis(string a)
{
stack< int > st;
for ( int i = 0; i < a.length(); i++) {
if (a[i] == '(' )
st.push(i);
else if (a[i] == ')' ) {
if (st.empty())
a.replace(i, 1, "-1" );
else {
a.replace(i, 1, "1" );
a.replace(st.top(), 1, "0" );
st.pop();
}
}
}
while (!st.empty()) {
a.replace(st.top(), 1, "-1" );
st.pop();
}
cout << a << endl;
}
int main()
{
string str = "(a))" ;
identifyParenthesis(str);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static void identifyParenthesis(StringBuffer a)
{
Stack<Integer> st = new Stack<Integer>();
for ( int i = 0 ; i < a.length(); i++)
{
if (a.charAt(i) == '(' )
st.push(i);
else if (a.charAt(i) == ')' )
{
if (st.empty())
a.replace(i, i + 1 , "-1" );
else
{
a.replace(i, i + 1 , "1" );
a.replace(st.peek(), st.peek() + 1 , "0" );
st.pop();
}
}
}
while (!st.empty())
{
a.replace(st.peek(), 1 , "-1" );
st.pop();
}
System.out.println(a);
}
public static void main(String[] args)
{
StringBuffer str = new StringBuffer( "(a))" );
identifyParenthesis(str);
}
}
|
Python3
def identifyParenthesis(a):
st = []
for i in range ( len (a)):
if (a[i] = = '(' ):
st.append(a[i])
elif (a[i] = = ')' ):
if ( len (st) = = 0 ):
a = a.replace(a[i], "-1" , 1 )
else :
a = a.replace(a[i], "1" , 1 )
a = a.replace(st[ - 1 ], "0" , 1 )
st.pop()
while ( len (st) ! = 0 ):
a = a.replace(st[ - 1 ], 1 , "-1" );
st.pop()
print (a)
if __name__ = = "__main__" :
st = "(a))"
identifyParenthesis(st)
|
C#
using System;
using System.Collections.Generic;
class GFG {
static void identifyParenthesis( string a)
{
Stack< int > st = new Stack< int >();
for ( int i = 0; i < a.Length; i++)
{
if (a[i] == '(' )
st.Push(i);
else if (a[i] == ')' )
{
if (st.Count == 0)
{
a = a.Substring(0, i) + "-1" + a.Substring(i + 1);
}
else
{
a = a.Substring(0, i) + "1" + a.Substring(i + 1);
a = a.Substring(0, st.Peek()) + "0" + a.Substring(st.Peek() + 1);
st.Pop();
}
}
}
while (st.Count > 0)
{
a = a.Substring(0, st.Peek()) + "-1" + a.Substring(st.Peek() + 1);
st.Pop();
}
Console.Write( new string (a));
}
static void Main() {
string str = "(a))" ;
identifyParenthesis(str);
}
}
|
Javascript
<script>
function identifyParenthesis(a)
{
let st = [];
for (let i = 0; i < a.length; i++)
{
if (a[i] == '(' )
st.push(i);
else if (a[i] == ')' )
{
if (st.length == 0)
{
a[i] = "-1" ;
}
else
{
a[i] = "1" ;
a[st[st.length - 1]] = "0" ;
st.pop();
}
}
}
while (st.length > 0)
{
a[st[st.length - 1]] = "-1" ;
st.pop();
}
document.write(a.join( "" ));
}
let str = "(a))" ;
identifyParenthesis(str.split( '' ));
</script>
|
Output:
0a1-1
Time Complexity: O(n)
Auxiliary Space : O(n)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
15 Jun, 2022
Like Article
Save Article