VOOZH about

URL: https://www.geeksforgeeks.org/dsa/evaluate-a-boolean-expression-represented-as-string/

⇱ Evaluate a boolean expression represented as string - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Evaluate a boolean expression represented as string

Last Updated : 15 Jul, 2022

Given a string consisting of only 0, 1, A, B, C where 
A = AND 
B = OR 
C = XOR 
Calculate the value of the string assuming no order of precedence and evaluation is done from left to right.
Constraints – The length of string will be odd. It will always be a valid string. 
Example, 1AA0 will not be given as an input.

Examples: 

Input : 1A0B1
Output : 1
1 AND 0 OR 1 = 1

Input : 1C1B1B0A0
Output : 0

Source : Microsoft online round for internship 2017

The idea is to traverse all operands by jumping a character after every iteration. For current operand str[i], check values of str[i+1] and str[i+2], accordingly decide the value of current subexpression.

Implementation:


Output
0

Time Complexity: O(n)
Auxiliary Space: O(n) 

Comment
Article Tags:
Article Tags: