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.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Evaluate an array expression with numbers, + and -
- Divide large number represented as string
- Square of large number represented as String
- Reverse all the word in a String represented as a Linked List
- Product of nodes at k-th level in a tree represented as string
- Product of nodes at k-th level in a tree represented as string using Recursion
- Parsing String of symbols to Expression
- Solve the Logical Expression given by string
- How to check string is alphanumeric or not using Regular Expression
- 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
- Add two numbers represented by two arrays
- Add two numbers represented by linked lists | Set 2
- Add two numbers represented by linked lists | Set 1
- Boolean Parenthesization Problem | DP-37
- Multiply Large Numbers represented as Strings
- Find winner of an election where votes are represented as candidate names