Brute Force Intuition
The simple and brute force approach is to try to put brackets in all possible ways, then check how many different ways you can put brackets and how many of them result in true.
Basically, weâ€™ll break down the â€˜EXPâ€™ into smaller expressions and see how many ways that smaller expression can return True and how many ways a smaller expression can return False. Then according to the operator, we can club these small answers to find out the number of expressions that result in true. It will become more clear as we proceed.
Algorithm
Letâ€™s create a recursive function called â€˜findWays()â€™ which will take 4 parameters â€˜EXPâ€™ as our given expression, â€˜STâ€™ represents the starting point of subexpression(smaller part), â€˜ENDâ€™ represents the ending point of subexpression(smaller part), â€˜IS_TRUEâ€™ stores whether the subexpression should be evaluated to true or false.
Base Conditions
 If â€˜STâ€™ is greater than â€˜END,â€™ then return False.

If â€˜STâ€™ equals â€˜END,â€™ it means there is only one character left in the â€˜EXPâ€™ then we will check for the following:
 If the value of â€˜IS_TRUEâ€™ is True, then we will check if the current element is true or not, i.e., â€˜EXP[ST]â€™. If it is true, then we will return true else, we will return false.
 If the value of â€˜IS TRUEâ€™ is False, then we will return the opposite of what we have returned above, i.e., if â€˜EXP[ST]' is false, we will return True else, we will return False.
Recursive calls
 We will create a new variable, â€˜ANSâ€™, which will store our final answer. Weâ€™ll initialize it to zero.

We will now loop over â€˜EXPâ€™ using variable â€˜Kâ€™ in the range â€˜ST' + 1 <= â€˜K' <= â€˜END'  1 and do the following:
 We will create four variables: â€˜LEFT_TRUEâ€™, which will store the number of ways in which the expression to the left of â€˜K' is true, â€˜LEFT_FALSEâ€™ will store the number of ways in which the expression to the left of â€˜K' is false,â€˜RIGHT_TRUEâ€™ will store the number of ways the expression to the right to â€˜K' is true and finally â€˜RIGHT_FALSE' will store the number of ways in which the expression to the right of â€˜K' is true.
 We'll find these values by using the calling â€˜findWays()â€™ function recursively in the following ways. â€˜ST' will be â€˜ST,' and â€˜END' will be changed to â€˜K'  1 to make a call to the left part, and to reach the right part, we will change â€˜ST' to â€˜K' + 1 and â€˜END' will remain â€˜ENDâ€™. Similarly, If we need to find the number of ways for true, set the value of â€˜IS_TRUE' to True; otherwise, put it to False.

Now, depending on the value of â€˜IS_TRUE' and the value of the current operator (â€˜EXP[K]'), there will be numerous combinations possible. For understanding one such case, Let us assume that â€˜EXP[K]â€™ is  (â€˜ORâ€™) and â€˜IS_TRUE' = True, then using the following truth table of â€˜ORâ€™ we can calculate our answer.
 T  T = T
 F  T = T
 T  F = T
 F  F = F
 Thus, â€˜ANS' = â€˜LEFT_TRUEâ€™ * â€™RIGHT_FALSE' + â€˜LEFT_FALSE' * â€™RIGHT_TRUE'+ â€˜LEFT_TRUE' * â€™RIGHT_TRUE' + â€™ANSâ€™.Here we are adding only those cases which will result in true
 We can do the same for other possible combinations as well.
 Return the variable â€˜ANSâ€™ as it now contains our final answer.
Now letâ€™s look at the code for the above algorithm.
Code
#include<iostream>
#include<vector>
#include<string>
using namespace std;
#define mod 1000000007
int findWays(string &exp, int i, int j, int isTrue) {
// Corner Cases.
if (i > j) {
return 0;
}
// If the length of expression is 1, we need to evaluate its value.
if (i == j) {
if (isTrue) {
return exp[i] == 'T' ? 1 : 0;
} else {
return exp[i] == 'F' ? 1 : 0;
}
}
long long int ans = 0;
for (int k = i + 1; k <= j  1; k += 2) {
// The number of ways expression left to 'K' will be true.
long long int leftTrue = (findWays(exp, i, k  1, 1)) % mod;
// The number of ways expression left to 'K' will be false.
long long int leftFalse = (findWays(exp, i, k  1, 0)) % mod;
// The number of ways expression right to 'K' will be true.
long long int rightTrue = (findWays(exp, k + 1, j, 1)) % mod;
// The number of ways expression right to 'K' will be false.
long long int rightFalse = (findWays(exp, k + 1, j, 0)) % mod;
if (exp[k] == '') {
// T  T = T, T  F = T, F  T = T , F  F = F.
if (isTrue) {
ans += leftTrue * rightTrue + leftTrue *
rightFalse + leftFalse * rightTrue;
ans = ans % mod;
} else {
ans += leftFalse * rightFalse;
ans = ans % mod;
}
}
else if (exp[k] == '&') {
// T & T = T, T & F = F, F & T = F , F  F = F.
if (isTrue) {
ans += leftTrue * rightTrue;
ans = ans % mod;
} else {
ans += leftTrue * rightFalse + leftFalse *
rightTrue + leftFalse * rightFalse;
ans = ans % mod;
}
}
else {
// T ^ T = F, T ^ F = T, F ^ T = T , F ^ F = F.
if (isTrue) {
ans += leftTrue * rightFalse + leftFalse *
rightTrue;
ans = ans % mod;
} else {
ans += leftTrue * rightTrue + leftFalse *
rightFalse;
ans = ans % mod;
}
}
}
return ans;
}
int evaluateExp(string & exp) {
int n = exp.length();
// We need to evaluate the whole expression for true.
return findWays(exp, 0, n  1, 1);
}
int main() {
// Input String.
string s;
cin >> s;
// Calling function and printing the answer.
cout << evaluateExp(s) << endl;
}
Input
T^T^F
Output
0
Time Complexity
O(4 ^ N), Where â€˜Nâ€™ is the length of the string.
At each step, we are making 4 calls and at that particular call, we are making 4 calls again thus, bringing the time complexity to O(4^N).
Space Complexity
O(4^ N), Where â€˜Nâ€™ is the length of the string.
As this is the amount of space that the recursion stack will utilize to store 4 ^ N calls.
Memoization Intuition
Our previous approach was pretty simple and straightforward, but the time complexity was unacceptable. But if we closely look at the subproblems, we can clearly see that we are solving the same subproblem again and again. Thus by addressing the overlapping subproblems, we can improve our solution. As a result, keeping the same subproblems in a DP table will reduce the requirement to solve them repeatedly. This is also known as topdown dynamic programming.
Algorithm
We'll start by creating a 3D DP array â€˜MEMO[N][N][2]â€™ where â€˜Nâ€™ is the length of the string â€˜EXP.â€™ Initially, all states are unexplored, we will initialize the table to 1.
MEMO[i][j][k] = 1 indicates that the current state is not exploredwhereas, â€˜MEMO[i][j][0]â€™ stores the number of times expression(i, j) is false, while â€˜MEMO[i][j][1]â€™ stores the number of times expression(i, j) is True. Here â€˜iâ€™ is the starting position of the â€˜EXPâ€™ while â€˜jâ€™ is the ending position on â€˜EXPâ€™.
Thus, there will be two changes in the recursive approach:
 In the base case, we will first check if the value of â€˜MEMO' is 1, and if it is not, which means the answer to this subproblem has already been calculated, we will simply return the value contained in the â€˜MEMOâ€™ corresponding to that particular state.
 If the value in â€˜MEMOâ€™ is 1, it indicates that the answer to that particular state does not exist. Therefore, we will store the value of â€˜ANSâ€™ in â€˜MEMO' before returning it.
Now letâ€™s look at the code for the above approach.
Code
#include<iostream>
#include<vector>
#include<string>
using namespace std;
#define mod 1000000007
vector<vector<vector<long long int >>> memo;
int findWays(string &exp, int i, int j, int isTrue) {
// Corner Cases.
if (i > j) {
return 0;
}
// If the length of expression is 1, we need to evaluate its value.
if (i == j) {
if (isTrue) {
return exp[i] == 'T' ? 1 : 0;
} else {
return exp[i] == 'F' ? 1 : 0;
}
}
if (memo[i][j][isTrue] != 1) {
return memo[i][j][isTrue];
}
long long int ans = 0;
for (int k = i + 1; k <= j  1; k += 2) {
if (memo[i][k  1][1] == 1) {
memo[i][k  1][1] = (findWays(exp, i, k  1, 1)) % mod;
}
if (memo[i][k  1][0] == 1) {
memo[i][k  1][0] = (findWays(exp, i, k  1, 0)) % mod;
}
if (memo[k + 1][j][1] == 1) {
memo[k + 1][j][1] = (findWays(exp, k + 1, j, 1)) % mod;
}
if (memo[k + 1][j][0] == 1) {
memo[k + 1][j][0] = (findWays(exp, k + 1, j, 0)) % mod;
}
// The number of ways expression left to 'K' will be true.
long long int leftTrue = memo[i][k  1][1];
// The number of ways expression left to 'K' will be false.
long long int leftFalse = memo[i][k  1][0];
// The number of ways expression right to 'K' will be true.
long long int rightTrue = memo[k + 1][j][1];
// The number of ways expression right to 'K' will be false.
long long int rightFalse = memo[k + 1][j][0];
if (exp[k] == '') {
// T  T = T, T  F = T, F  T = T, F  F = F.
if (isTrue) {
ans += leftTrue * rightTrue + leftTrue *
rightFalse + leftFalse * rightTrue;
ans = ans % mod;
} else {
ans += leftFalse * rightFalse;
ans = ans % mod;
}
}
else if (exp[k] == '&') {
// T & T = T, T & F = F, F & T = F, F  F = F.
if (isTrue) {
ans += leftTrue * rightTrue;
ans = ans % mod;
} else {
ans += leftTrue * rightFalse + leftFalse *
rightTrue + leftFalse * rightFalse;
ans = ans % mod;
}
}
else {
// T ^ T = F, T ^ F = T, F ^ T = T, F ^ F = F.
if (isTrue) {
ans += leftTrue * rightFalse + leftFalse * rightTrue;
ans = ans % mod;
} else {
ans += leftTrue * rightTrue + leftFalse * rightFalse;
ans = ans % mod;
}
}
}
return memo[i][j][isTrue] = ans;
}
int evaluateExp(string &exp) {
int n = exp.length();
memo = vector<vector<vector<long long int>>> (n, vector<vector<long long int>>(n, vector<long long int>(2, 1)));
// We need to evaluate the whole expression for true.
return findWays(exp, 0, n  1, 1);
}
int main() {
// Input String.
string s;
cin >> s;
// Calling function and printing the answer.
cout << evaluateExp(s) << endl;
}
Input
T^T^F
Output
Input
Time Complexity
O(N ^ 3), where â€˜Nâ€™ is the length of the given string.
In the worstcase scenario, after performing N ^ 3 calls, all of the states will be investigated, and we will be able to use the â€˜MEMOâ€™ result to find our final answer.
Space Complexity
O(N^2), where â€˜Nâ€™ is the length of the given string.
As a 3D array of size N * N * 2 is being used.
BottomUp Intuition
In the bottomup approach, we'll start by creating a 3D array called 'DP[N][N][2]â€™ and initialize it to 0, where â€˜Nâ€™ is the length of the string 'EXP.' The number of ways expression(i, j) will be false will be stored in DP[i][j][0], whereas the number of ways expression(i, j) will be True will be stored in DP[i][j][1]. Here â€˜iâ€™ is the starting position of the â€˜EXPâ€™ while â€˜jâ€™ is the ending position on â€˜EXPâ€™.
Algorithm
 Firstly we will fill the diagonals by using a variable â€˜iâ€™ we can check if the â€˜EXP[i]â€™ is true then the diagonal entry for the true one will be true otherwise for the false one it will be true.

Weâ€™ll use the variable â€˜GAPâ€™ to iterate over the string â€˜EXP' in the range of 2 <= â€˜GAP'< N times and will keep increasing â€˜GAP' by 2 each time, and do the following:

Weâ€™ll use the variable â€˜jâ€™ to iterate through the string â€˜EXP' in the range of 2 < = â€˜j' < N, and will keep increasing â€˜j' by 2 each time, and do the following:

Weâ€™ll use the variable â€˜kâ€™ to iterate through the string â€˜EXP' in the range of 2 < = â€˜kâ€™ < N, and will keep increasing â€˜k' by 2 each time, and assign the following values to the following variables:
 'LEFT_TRUEâ€™ = DP[j][k][1]
 'LEFT_FALSEâ€™ = DP[j][k][0]
 'RIGHT_TRUEâ€™ = DP[k + 2][j + GAP][1]
 'RIGHT_FALSEâ€™ = DP[k + 2][j + GAP[0]]

Then we will check which operator is there at â€˜Kâ€™ + 1 place. For example, letâ€™s say if it is OR(â€˜â€™) then we will assign values in the following way:
 DP[j][j + GAP][1] = 'LEFT_TRUE' * â€˜RIGHT_FALSEâ€™ + 'LEFT _FALSE' * â€˜RIGHT_TRUEâ€™ + 'LEFT TRUE' * 'RIGHT TRUE' + DP[j][j + GAP][1].
 DP[j][j + GAP][0] += 'RIGHT FALSE' * 'LEFT FALSE' + DP[j][j + GAP][0]. We can solve for other operators in the same way.
3. Return DP[0][N  1][1] as our final answer.
Below is the implementation of the above approach.
Code
#include<iostream>
#include<vector>
#include<string>
using namespace std;
#define mod 1000000007
int evaluateExp(string &exp) {
int n = exp.length();
// We need to evaluate the whole expression for true.
vector<vector<vector<long long int>>> dp(n, vector< vector<long long int>> (n, vector<long long int> (2, 0)));
// Filling the diagonal entries.
for (int i = 0; i < n; i++) {
if (exp[i] == 'T') {
dp[i][i][1] = 1;
} else if (exp[i] == 'F') {
dp[i][i][0] = 1;
}
}
// Filling the dp array.
for (int gap = 2; gap < n; gap += 2) {
for (int j = 0; j + gap < n; j += 2) {
for (int k = j; k < j + gap; k += 2) {
if(exp[k + 1] == '') {
// T  T = T, T  F = T, F  T = T, F  F = F.
dp[j][j + gap][1] += ((dp[j][k][0] * dp[k + 2][j +
gap][1]) + (dp[j][k][1] * dp[k + 2][j + gap][0]) +
(dp[j][k][1] * dp[k + 2][j + gap][1])) % mod;
dp[j][j + gap][1] %= mod;
dp[j][j + gap][0] += ((dp[j][k][0] * dp[k + 2][j +
gap][0])) % mod;
dp[j][j + gap][0] %= mod;
}
if(exp[k + 1] == '&') {
// T & T = T, T & F = F, F & T = F , F  F = F.
dp[j][j + gap][1] += ((dp[j][k][1] * dp[k + 2][j +
gap][1])) % mod;
dp[j][j + gap][1] %= mod;
dp[j][j + gap][0] += ((dp[j][k][0] * dp[k + 2][j +
gap][1]) + (dp[j][k][1] * dp[k + 2][j + gap][0]) +
(dp[j][k][0] * dp[k + 2][j + gap][0])) % mod;
dp[j][j + gap][0] %= mod;
}
if(exp[k + 1] == '^') {
// T ^ T = F, T ^ F = T, F ^ T = T, F ^ F = F
dp[j][j + gap][1] += ((dp[j][k][1] * dp[k + 2][j +
gap][0]) + (dp[j][k][0] * dp[k + 2][j + gap][1])) %
mod;
dp[j][j + gap][1] %= mod;
dp[j][j + gap][0] += ((dp[j][k][0] * dp[k + 2][j +
gap][0]) + (dp[j][k][1] * dp[k + 2][j + gap][1])) %
mod;
dp[j][j + gap][0] %= mod;
}
}
}
}
return dp[0][n  1][1];
}
int main() {
// Input String.
string s;
cin >> s;
// Calling function and printing the answer.
cout << evaluateExp(s) << endl;
}
Input
T^T^F
Output
Input
Time Complexity
O(N ^ 3), where â€˜Nâ€™ is the length of the given string.
As we are iterating over a 3D array and using 3 nested loops.
Space complexity
O(N ^ 2), where â€˜Nâ€™ is the length of the given string.
As a 3D array of size N * N * 2 is being used.
Check out this problem  Longest String Chain
Key Takeaways
We saw how we built the solution from recursion to memoization and finally to DP for the problem evaluate expression to true. Now, Understanding a new concept always makes one excited, and this excitement leads to learning more concepts. Weâ€™ll make that simple for you, head over to our practice platform Coding Ninjas Studio to practice top problems like evaluate expression to true, attempt mock tests, read interview experiences, and many more. Till then, Happy Coding!