Last Updated: 28 Feb, 2021

# Boolean Evaluation

Moderate

## Problem statement

#### Note :

``````‘T’ will represent the operand TRUE.
‘F’ will represent the operand FALSE.
‘|’ will represent the operator OR.
‘&’ will represent the operator AND.
‘^’ will represent the operator XOR.
``````

#### Example :

``````Input: 'exp’ = "T|T & F".

Output: 1

Explanation:
There are total 2  ways to parenthesize this expression:
(i) (T | T) & (F) = F
(ii) (T) | (T & F) = T
Out of 2 ways, one will result in True, so we will return 1.
``````

#### Input format:

``````The first line of each input contains a string representing the expression ‘exp’.
``````

#### Output format:

``````print a single integer representing the number of ways we can parenthesize the expression to evaluate to true.
``````
##### Note:
``````You do not need to print anything, it has already been taken care of. Just implement the given function.
``````

## Approaches

### 01 Approach

Approach: Basically, we will divide the expression into smaller parts and will try to find the number of ways a smaller expression can result in True and the number of ways a smaller expression can result in False.

With the help of the result of the smaller expression, we will be able to find the number of ways the whole expression can result in True.

Let’s define a recursive function, ‘findWays’, which will receive the following parameter.

(i) ‘exp’ the given expression.

(ii) ‘st’ is an integer variable representing the starting point for the sub-expression.

(iii) ‘end’ is an integer variable representing the ending point for the sub-expression.

(iii) ‘isTrue’ is a bool variable representing whether the sub-expression should evaluate to True or False.

#### Base Condition :

1. If  ‘st’ >  ‘end’ then do:
• Return False.
2. If  ‘st’ is equal to  ‘end’ then do:
• If ‘isTrue’  is equal to True :
• If current element is true or ‘exp[st]’ == ‘T’
• Return True
• Else
• Return False.
• If ‘isTrue’  is equal to False :
• If current element is False or ‘exp[st]’ == ‘F’
• Return True.
• Else
• Return False.

#### Recursive Calls:

1. Initialize an integer variable ‘ans’=0.
2. Run a loop over ‘exp’ for ‘st’+1 <= ‘k’ <= 'end’-1 and do:
• We will need to find out the value of the below variables
• ‘leftTrue’  = The number of ways the expression left to ‘k’ will be true.
• ‘leftFalse’  = The number of ways the expression left to ‘k’ will be false.
• ‘rightTrue’  = The number of ways expression right to ‘k’ will be true:
• ‘rightFalse’  = The number of ways expression right to ‘k’ will be true:
• We will find these values by making calls to ‘findWays'.
• To make a call to the left part,  ‘st’ will be ‘st’, and  ‘end’ will be ‘k’-1.
• To make a call to the right part, ‘st’ will be ‘k’+1, and ‘end’ will be ‘end’.
• Set the value of ‘isTrue’ = True if we need to find out the number of ways for true else, set False.
3. Now there will be many combinations according to the value of ‘isTrue’ and the value of the current operator(‘exp[k]’).
4. Let’s find out what value will current sub-expression add to ‘ans’ for the given operator ‘|’ and ‘isTrue’ = True.
5. The truth table for OR :
• T|T=T
• F|T=T
• T|F=T
• F|F=F
6. So ‘ans’+= ‘leftTrue’*‘rightFalse’ +‘leftFalse’ *‘rightTrue’ +‘leftTrue’*‘rightTrue’
7. Similarly, we can find’ for other operators.
8. Return ‘ans’.