Check if two expressions with brackets are same
You are given two strings which are expressions in variables. You need to compare and tell if they are similar or different. You need to return “YES” for the same expressions and “NO” for different expressions. The expressions consist of lowercase alphabets, ‘+’, ‘-’ and ‘(‘, ‘)’. It may be assumed that there are at most 26 operands from ‘a’ to ‘z’.
Two expressions will be said as similar if they have the same set of operands and when some values are given to these operands, then both the expressions give the same result.
For example:
If the first expression is: (a + (b + c)) and the second expression is: a + b + c. The expressions are the same because if they are evaluated, they will give the same output. So the answer is “YES”.
Note:
You can assume that any expression is evaluated according to the BODMAS rule.
Input Format:
The first line of input contains a single integer T, representing the number of test cases.
Then the T test cases follow.
The first line of each test case contains a string S1 denoting the first expression.
The second line of each test case contains a string S2 denoting the second expression.
Output Format:
For each test case, print “YES” if the expressions are the same and “NO” if the expressions are not the same, on a separate line.
Note:
You are not required to print the expected output, it has already been taken care of. Just implement the function.
Constraints:
1 ≤ T ≤ 10
1 ≤ |S1|, |S2| ≤ 1000
Time Limit : 1 sec
CodingNinjas
author
2y
Optimal Solution
- We keep track of the current sign, whether it is + or -
- For example, the expression: a-(-b+c) evaluates to a+b-c. Here the sign of b and c are changed in the final expression.
- We maintai...read more
Help your peers!
Add answer anonymously...
Top 42Gears Mobility Systems Mts1 interview questions & answers
Popular interview questions of Mts1
>
42Gears Mobility Systems Mts1 Interview Questions
Stay ahead in your career. Get AmbitionBox app
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
Get AmbitionBox app