Given string str representing a logical expression which consists of the operators | (OR), & (AND),! (NOT) , 0, 1 and, only (i.e. no space between characters). The task is to print the result of the logical expression.
Examples:
Input: str = “[[0,&,1],|,[!,1]]”
Output: 0
Explanation:
[[0,&,1],|,[!,1]]
[[0,&,1],|,0]
[[0,&,1],|,0]
[0,|,0]
[0,|,0]
[0]
0
Input: str = “[!,[[0,&,[!,1]],|,[!,[[!,0],&,1]]]]”
Output: 1
Approach:
- Start traversing the string from the end.
- If [ found go to Step-3 otherwise push the characters into the stack.
- Pop characters from the stack until the stack top becomes”]”. > Insert each popped character into the vector.
- If the stack top becomes ] after 5 pop operations then the vector will be x, |, y or x, &, y.
- If the stack top becomes ] after 3 pop operations then the vector will be !, x.
- Pop ] from the stack top.
- Perform the respective operations on the vector elements then push the result back into the stack.
- If the string is fully traversed then return the value at the stack top otherwise go to step 2.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
char logicalExpressionEvaluation(string str)
{
stack< char > arr;
for ( int i = str.length() - 1; i >= 0; i--)
{
if (str[i] == '[' )
{
vector< char > s;
while (arr.top() != ']' )
{
s.push_back(arr.top());
arr.pop();
}
arr.pop();
if (s.size() == 3)
{
s[2] == '1' ? arr.push( '0' ) : arr.push( '1' );
}
else if (s.size() == 5)
{
int a = s[0] - 48, b = s[4] - 48, c;
s[2] == '&' ? c = a && b : c = a || b;
arr.push(( char )c + 48);
}
}
else
{
arr.push(str[i]);
}
}
return arr.top();
}
int main()
{
string str = "[[0,&,1],|,[!,1]]" ;
cout << logicalExpressionEvaluation(str) << endl;
return 0;
}
|
Java
import java.util.*;
class GFG
{
static char logicalExpressionEvaluation(String str)
{
Stack<Character> arr = new Stack<Character>();
for ( int i = str.length() - 1 ; i >= 0 ; i--)
{
if (str.charAt(i) == '[' )
{
Vector<Character> s = new Stack<Character>();
while (arr.peek() != ']' )
{
s.add(arr.peek());
arr.pop();
}
arr.pop();
if (s.size() == 3 )
{
arr.push(s.get( 2 ) == '1' ? '0' : '1' );
}
else if (s.size() == 5 )
{
int a = s.get( 0 ) - 48 ,
b = s.get( 4 ) - 48 , c;
if (s.get( 2 ) == '&' )
{
c = a & b;
}
else
{
c = a | b;
}
arr.push(( char )(c + 48 ));
}
}
else
{
arr.push(str.charAt(i));
}
}
return arr.peek();
}
public static void main(String[] args)
{
String str = "[[0,&,1],|,[!,1]]" ;
System.out.println(logicalExpressionEvaluation(str));
}
}
|
Python3
import math as mt
def logicalExpressionEvaluation(string):
arr = list ()
n = len (string)
for i in range (n - 1 , - 1 , - 1 ):
if (string[i] = = "[" ):
s = list ()
while (arr[ - 1 ] ! = "]" ):
s.append(arr[ - 1 ])
arr.pop()
arr.pop()
if ( len (s) = = 3 ):
if s[ 2 ] = = "1" :
arr.append( "0" )
else :
arr.append( "1" )
elif ( len (s) = = 5 ):
a = int (s[ 0 ]) - 48
b = int (s[ 4 ]) - 48
c = 0
if s[ 2 ] = = "&" :
c = a & b
else :
c = a | b
arr.append((c) + 48 )
else :
arr.append(string[i])
return arr[ - 1 ]
string = "[[0,&,1],|,[!,1]]"
print (logicalExpressionEvaluation(string))
|
C#
using System;
using System.Collections.Generic;
public class GFG
{
static char logicalExpressionEvaluation(String str)
{
Stack< char > arr = new Stack< char >();
for ( int i = str.Length - 1; i >= 0; i--)
{
if (str[i] == '[' )
{
List< char > s = new List< char >();
while (arr.Peek() != ']' )
{
s.Add(arr.Peek());
arr.Pop();
}
arr.Pop();
if (s.Count == 3)
{
arr.Push(s[2] == '1' ? '0' : '1' );
}
else if (s.Count == 5)
{
int a = s[0] - 48,
b = s[4] - 48, c;
if (s[2] == '&' )
{
c = a & b;
}
else
{
c = a | b;
}
arr.Push(( char )(c + 48));
}
}
else
{
arr.Push(str[i]);
}
}
return arr.Peek();
}
public static void Main(String[] args)
{
String str = "[[0,&,1],|,[!,1]]" ;
Console.WriteLine(logicalExpressionEvaluation(str));
}
}
|
Javascript
using System;
using System.Collections.Generic;
public class GFG
{
static char logicalExpressionEvaluation(String str)
{
Stack<char> arr = new Stack<char>();
for (int i = str.Length - 1; i >= 0; i--)
{
if (str[i] == '[' )
{
List<char> s = new List<char>();
while (arr.Peek() != ']' )
{
s.Add(arr.Peek());
arr.Pop();
}
arr.Pop();
if (s.Count == 3)
{
arr.Push(s[2] == '1' ? '0' : '1' );
}
else if (s.Count == 5)
{
int a = s[0] - 48,
b = s[4] - 48, c;
if (s[2] == '&' )
{
c = a & b;
}
else
{
c = a | b;
}
arr.Push((char)(c + 48));
}
}
else
{
arr.Push(str[i]);
}
}
return arr.Peek();
}
public static void Main(String[] args)
{
String str = "[[0,&,1],|,[!,1]]" ;
Console.WriteLine(logicalExpressionEvaluation(str));
}
}
|
Complexity Analysis:
- Time Complexity: O(n) Here, n is the length of the string.
- Auxiliary Space: O(1)