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.
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.
This article is contributed by Ayushi Jain. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Evaluate an array expression with numbers, + and -
- Divide large number represented as string
- Square of large number represented as String
- Product of nodes at k-th level in a tree represented as string
- Solve the Logical Expression given by string
- Find all the patterns of "1(0+)1" in a given string | SET 2(Regular Expression Approach)
- Check if a binary string has a 0 between 1s or not | Set 2 (Regular Expression Approach)
- Check if a string contains only alphabets in Java using Lambda expression
- Check if a given string is a valid number (Integer or Floating Point) in Java | SET 2 (Regular Expression approach)
- Find if a given string can be represented from a substring by iterating the substring “n” times
- Boolean Parenthesization Problem | DP-37
- Find length of the largest region in Boolean Matrix
- Add two numbers represented by two arrays
- Add two numbers represented by linked lists | Set 1
- Add two numbers represented by linked lists | Set 2