21. Boolean Parenthesization

The problem can be found at the following link: Question Link

My Approach

  • Initialize a 3D array dp with dimensions n x n x 2 to store subproblem solutions.

  • Set the modulo value as 1003.

  • If i == j, set dp[i][j][1] to 1 if s[i] == 'T' and dp[i][j][0] to 1 if s[i] == 'F'.

  • Otherwise, initialize both dp[i][j][1] and dp[i][j][0] to 0.

  • Iterate over subproblems in a bottom-up manner from smaller subexpressions to the entire expression.

  • Use nested loops with indices i and j to represent the subexpression boundaries and k to iterate over possible operators between symbols.

  • Update values based on the operator:

  • If s[k+1] == '&', update dp[i][j][1] and dp[i][j][0] accordingly.

  • If s[k+1] == '|', update dp[i][j][1] and dp[i][j][0] accordingly.

  • If s[k+1] == '^', update dp[i][j][1] and dp[i][j][0] accordingly.

  • The final result is stored in dp[0][n-1][1], representing the number of ways the expression can be parenthesized to evaluate to true.

Time and Auxiliary Space Complexity

  • Time Complexity: O(N^3)

  • Auxiliary Space Complexity: O(N^2)

Code (C++)

Contribution and Support

For discussions, questions, or doubts related to this solution, please visit our discussion section. We welcome your input and aim to foster a collaborative learning environment.

If you find this solution helpful, consider supporting us by giving a ⭐ star to the getlost01/gfg-potd repository.

Last updated

Was this helpful?