Different Ways To Add Parenthesis

Suppose you have a string 'S' representing an arithmetic expression. Your task is to compute all potential outcomes by adding valid parentheses in different ways to evaluate the expression.

Example:

Input:
S = "2 * 3 - 2"
Output:
[4, 2] or [2, 4]
Explanation:

For the expression S = 2 * 3 - 2, the two valid computations are:
1. ((2 * 3) - 2) = 4
2. (2 * (3 - 2)) = 2

Input:

The first line contains an integer 'T', the number of test cases. 
The next 'T' lines each contain an arithmetic expression with numbers and operators '+', '-', '*'.

Output:

For each test case, output all the outcomes from the expression after adding valid parentheses. 
Results can be in any order, and each test case's results should be printed on a separate line.

Constraints:

  • 1 <= T <= 10
  • 3 <= len(S) <= 65
  • Each input expression 'S' must contain at least one operator.
  • Time limit: 1 second.

Note:

No need to print anything explicitly; focus on implementing the provided function.

Be the first one to answer
Add answer anonymously...
GoMechanic Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter