Given a string S consisting of opening parentheses, closing parentheses and integers, the task is to print Yes if, in this string, the integers correctly indicates its depth. Depth refers to the number of nested sets of parentheses surrounding that integer. Print No otherwise.
Examples:
Input: S = “((2)((3)))”
Output: Yes
Explanation:
No of Opening & Closing Parentheses around 2 = 2
No of Opening & Closing Parentheses around 3 = 3
Input: S = “((35)(2))”
Output: No
Explanation:
No of Opening & Closing Parentheses around 35 = 2
No of Opening & Closing Parentheses around 2 = 2
Approach:
- Iterate the string character by character
- If it is opening or closing parenthesis then append into an array arr[]
- If the character is digit, then iterate till the entire number is read and then append into the arr[]
- Iterate the array arr[] element by element
- If the element is ‘(‘ increment depth and open by 1
- If the element is ‘)’ decrement depth by 1 and increment close by 1
- If the element is an Integer, check if “Integer != depth”, if true
set flag to 0 and break the loop
- After iterating the entire string, check if open not equals to close, if true set flag = 0
Below is the implementation of the above approach
C++
#include <bits/stdc++.h>
using namespace std;
bool Formatted(string s)
{
vector< char > k;
int i = 0;
while (i < s.size())
{
if (s[i]== ')' or s[i]== '(' )
{
k.push_back(s[i]);
i += 1;
}
else
{
char st;
while (s[i]!= ')' and s[i]!= ')' )
{
st = s[i];
i = i + 1;
}
k.push_back(st);
}
}
int depth = 0, flag = 1;
int open = 0, close = 0;
for ( char i:k)
{
if (i == '(' )
{
depth += 1;
open += 1;
} else if (i == ')' ){
depth -= 1;
close += 1;
}
else {
if (i- '0' != depth){
flag = 0;
break ;
}
}
}
if (open != close)
flag = 0;
return (flag == 1)? true : false ;
}
int main()
{
string s = "((2)((3)))" ;
bool k = Formatted(s);
if (k == true )
printf ( "Yes" );
else
printf ( "No" );
return 0;
}
|
Java
import java.util.*;
class GFG{
static boolean Formatted(String s)
{
Vector<Character> k = new Vector<Character>();
int i = 0 ;
while (i < s.length())
{
if (s.charAt(i)== ')' || s.charAt(i)== '(' )
{
k.add(s.charAt(i));
i += 1 ;
}
else
{
char st = 0 ;
while (s.charAt(i)!= ')' && s.charAt(i)!= ')' )
{
st = s.charAt(i);
i = i + 1 ;
}
k.add(st);
}
}
int depth = 0 , flag = 1 ;
int open = 0 , close = 0 ;
for ( char i2 : k)
{
if (i2 == '(' )
{
depth += 1 ;
open += 1 ;
} else if (i2 == ')' ){
depth -= 1 ;
close += 1 ;
}
else {
if (i2- '0' != depth){
flag = 0 ;
break ;
}
}
}
if (open != close)
flag = 0 ;
return (flag == 1 )? true : false ;
}
public static void main(String[] args)
{
String s = "((2)((3)))" ;
boolean k = Formatted(s);
if (k == true )
System.out.printf( "Yes" );
else
System.out.printf( "No" );
}
}
|
Python3
def Formatted(s):
k = []
i = 0
while i < len (s):
if s[i].isdigit() = = False :
k.append(s[i])
i + = 1
else :
st = ""
while s[i].isdigit():
st + = s[i]
i = i + 1
k.append( int (st))
depth, flag = 0 , 1
open , close = 0 , 0
for i in k:
if i = = '(' :
depth + = 1
open + = 1
elif i = = ')' :
depth - = 1
close + = 1
else :
if i ! = depth:
flag = 0
break
if open ! = close:
flag = 0
return True if flag = = 1 else False
if __name__ = = '__main__' :
s = '((2)((3)))'
k = Formatted(s)
if k = = True :
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static bool Formatted(String s)
{
List< char > k = new List< char >();
int i = 0;
while (i < s.Length)
{
if (s[i]== ')' || s[i]== '(' )
{
k.Add(s[i]);
i += 1;
}
else
{
char st = '\x0000' ;
while (s[i] != ')' && s[i] != ')' )
{
st = s[i];
i = i + 1;
}
k.Add(st);
}
}
int depth = 0, flag = 1;
int open = 0, close = 0;
foreach ( char i2 in k)
{
if (i2 == '(' )
{
depth += 1;
open += 1;
} else if (i2 == ')' ){
depth -= 1;
close += 1;
}
else {
if (i2- '0' != depth){
flag = 0;
break ;
}
}
}
if (open != close)
flag = 0;
return (flag == 1)? true : false ;
}
public static void Main(String[] args)
{
String s = "((2)((3)))" ;
bool k = Formatted(s);
if (k == true )
Console.Write( "Yes" );
else
Console.Write( "No" );
}
}
|
Javascript
<script>
function Formatted(s)
{
let k = [];
let i = 0;
while (i < s.length)
{
if (s[i]== ')' || s[i]== '(' )
{
k.push(s[i]);
i += 1;
}
else
{
let st = 0;
while (s[i]!= ')' && s[i]!= ')' )
{
st = s[i];
i = i + 1;
}
k.push(st);
}
}
let depth = 0, flag = 1;
let open = 0, close = 0;
for (let i2=0;i2< k.length;i2++)
{
if (k[i2] == '(' )
{
depth += 1;
open += 1;
} else if (k[i2] == ')' ){
depth -= 1;
close += 1;
}
else {
if (k[i2]- '0' != depth){
flag = 0;
break ;
}
}
}
if (open != close)
flag = 0;
return (flag == 1)? true : false ;
}
let s = "((2)((3)))" ;
let k = Formatted(s);
if (k == true )
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(n), where n is the length of the given string.