Problem Statement:
Difficulty: Cakewalk <--> Easy
Prerequisites: Concept of Stacks, Reverse Polish Notation
The Problem:
The problem finally boils down to:
- You are given an algebraic expression
- Operations over one-character variables
- All expressions are closed by '(' and ')' brackets.
- Keeping track of secondary expressions between one opening '(' and its corresponding closing ')'
- Thus tertiary expressions in between secondary brackets and so on..
- Use of basic mathematical operation symbols (+ - / * ^ %)
Explanation:
All variables used in the problem are of length=1. It is assured that expressions are of type:simple. Implies that there will be expressions only of the type a+b and not a+b+c.
Now, maintain a count. For your input expression to terminate, you have to come across a reverse bracket id est ')'. In other words, for every ( there has to be a ). Thus you can start by taking in a character which you are sure is '('. Maintain a count and increment it for every '(' that you find along the way and while this count is more than zero, you must take in and process the expression. Now, a '(' can be followed by either another ( or some variable, say 'x'. Let's say you have gone through one or more of these '(' and have now come to a variable. You shall then push this variable into a solution array of length [405].
After a variable, you will get an operator symbol. You have to sideline it temporarily for one step. In other words you will put it into a stack and pull it out after you take the next variable into consideration. Suppose the next variable was 'y' {initial was 'x', right?}. Let your expression be x#y where # is some operation symbol. After taking ing 'x' you expect a symbol and you push it into a stack. Then you expect a character y. You push the character into the solution array. And then comes the symbol. The brackets are to be completely ignored for our answer.
Now take a different case. Exempli gratia, ((a-b)***(c/d)). Now how will you cope with the multiplication sign which has no variables but two expressions to follow? This is why, a symbol must necessarily be pushed to the solution array only after encountering the ')' closing bracket. Now take this specific case according to how our algorithm will process it.
- '(' --> Count = 1
- '(' --> Count = 2
- solution[0] = 'a'
- stack <-- 'minus' symbol
- solution1 = 'b'
- Encounter ')', count = 1, push stack top item into soln.
- solution2 = stack(top item)
- encounter symbol 'multiplication' <-- push to stack
- '(' --> count = 2
- steps 3 to 5 repeated somewhat [the division sign: c/d was pushed to stack]
- encountered ')' remove top item from stack '/' and push to solution[5] & count = 1
- encountered another ')': now remember the multiplication symbol that we pushed.. Now take that out from stack as it is the topmost item in our stack currently.
- count becomes 0. terminate.
Let's see how our solution array looks like. ab-cd/*
General Algorithm:
- initialize count=0;
- input first character (which you know is a opening bracket '(' for sure)
count ++
while count is greater than 0
do:
- if next char is '(' count ++
- else if next char is {+ - / .. etc} then put into stack
- else if next char is ')' count -- and then pull out topmost item from stack and push to array.
- (now only case left is variable alphabet): else directly push char to solution.
Solution
Editorialist solution can be foundHERE
Editorial By@aalok_sathe