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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Evaluate an array expression with numbers, + and -
- Find if a given string can be represented from a substring by iterating the substring “n” times
- Divide large number represented as string
- Product of nodes at k-th level in a tree represented as string
- Square of large number represented as String
- Product of nodes at k-th level in a tree represented as string using Recursion
- Reverse all the word in a String represented as a Linked List
- How to convert a string to boolean in Java
- Check if a given string is a valid number (Integer or Floating Point) in Java | SET 2 (Regular Expression approach)
- 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
- Solve the Logical Expression given by string
- Parsing String of symbols to Expression
- How to check string is alphanumeric or not using Regular Expression
- Add two numbers represented by linked lists | Set 1
- Add two numbers represented by linked lists | Set 2
- Multiply Large Numbers represented as Strings
- Find winner of an election where votes are represented as candidate names
- Add two numbers represented by two arrays