Given two expressions in the form of strings. The task is to compare them and check if they are similar. Expressions consist of lowercase alphabets, ‘+’, ‘-‘ and ‘( )’.
Input : exp1 = "-(a+b+c)" exp2 = "-a-b-c" Output : Yes Input : exp1 = "-(c+b+a)" exp2 = "-c-b-a" Output : Yes Input : exp1 = "a-b-(c-d)" exp2 = "a-b-c-d" Output : No
It may be assumed that there are at most 26 operands from ‘a’ to ‘z’ and every operand appears only once.
A simple idea behind is to keep a record of the Global and Local Sign(+/-) through the expression. The Global Sign here means the multiplicative sign at each operand. The resultant sign for an operand is local sign multiplied by the global sign at that operand.
For example, the expression a+b-(c-d) is evaluated as (+)+a(+)+b(-)+c(-)-d => a + b – c + d. The global sign (represented inside bracket) is multiplied to the local sign for each operand.
In the given solution, stack is used to keep record of the global signs. A count vector records the counts of the operands(lowercase Latin letters here). Two expressions are evaluated in opposite manners and finally, it is checked if the all entries in the count vector are zeros.
This article is contributed by Amol Mejari. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Printing brackets in Matrix Chain Multiplication Problem
- Find an equal point in a string of brackets
- Remove brackets from an algebraic string containing + and - operators
- Number of closing brackets needed to complete a regular bracket sequence
- Binary tree to string with brackets
- Balance a string after removing extra brackets
- Check whether two strings contain same characters in same order
- Evaluation of Prefix Expressions
- Print all the possible arithmetic expressions for a given number
- How to validate a Password using Regular Expressions in Java
- Meta Strings (Check if two strings can become same after a swap in one string)
- Check if two BSTs contain same set of elements
- Check if two same sub-sequences exist in a string or not
- Program to check if two strings are same or not
- Check if two strings are same ignoring their cases
- Check if the two given stacks are same
- Check if all bits can be made same by flipping two consecutive bits
- Check if a string can be split into two strings with same number of K-frequent characters
- Check if a palindromic string can be obtained by concatenating substrings split from same indices of two given strings
- Median of two sorted arrays of same size
Improved By : sanjeev2552