Given a mathematical equation that contains only numbers and +, -, *, /. Print the equation in reverse, such that the equation is reversed, but the numbers remain the same.
It is guaranteed that the given equation is valid, and there are no leading zeros.
Example 1:
Input:
S = "20-3+5*2"
Output: 2*5+3-20
Explanation: The equation is reversed with
numbers remaining the same.
Example 2:
Input: 
S = "5+2*56-2/4"
Output: 4/2-56*2+5
Explanation: The equation is reversed with
numbers remaining the same.
Your Task:
You don't need to read input or print anything. Your task is to complete the function reverseEqn() which takes the string S representing the equation as input and returns the resultant string representing the equation in reverse.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(|S|).
Constraints:
1<=|S|<=10^{5}
The string contains only the characters '0' - '9', '+', '-', '*', and '/'.

Source: geeksforgeeks | Difficulty: EASY
Tags: String algorithms

Data structures

Starter code: #User function Template for python3



class Solution:

    def reverseEqn(self, s):

        # code here

Solutions:
class Solution:

	def reverseEqn(self, s):
		arr = list(s)
		nums = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
		temp = []
		ans = []
		for v in arr:
			if v in nums:
				temp.append(v)
			else:
				temp = ''.join(temp)
				ans.insert(0, temp)
				ans.insert(0, v)
				temp = []
		temp = ''.join(temp)
		ans.insert(0, temp)
		ans = ''.join(ans)
		return ans


class Solution:

	def reverseEqn(self, s):
		st = []
		sr = ''
		for i in s:
			if i == '-' or i == '+' or i == '/' or (i == '*'):
				st.append(i)
				sr += '@'
			else:
				sr += i
		a = sr.split('@')
		a = a[::-1]
		newsr = ''
		for i in a:
			newsr = newsr + i
			if st:
				b = st.pop(-1)
				newsr = newsr + b
		return newsr


class Solution:

	def reverseEqn(self, s):
		result = ''
		num_stk = []
		opt_stk = []
		num_str = ''
		for i in s:
			if i.isnumeric():
				num_str += i
			else:
				if num_str:
					num_stk.append(num_str)
					num_str = ''
				opt_stk.append(i)
			result = num_str
		for i in range(len(opt_stk)):
			result += opt_stk.pop()
			result += num_stk.pop()
		return result


class Solution:

	def reverseEqn(self, s):
		result = ''
		stack = ''
		for item in s:
			if item.isdigit():
				stack += item
			else:
				result = item + stack + result
				stack = ''
		return stack + result


class Solution:

	def reverseEqn(self, s):
		stk = []
		r = ''
		nums = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0']
		for i in range(len(s) - 1, -1, -1):
			if s[i] in nums:
				stk.append(s[i])
			else:
				while stk:
					r += stk.pop()
				r = r + s[i]
		while stk:
			r += stk.pop()
		return r


class Solution:

	def reverseEqn(self, s):
		t = ''
		l = []
		for i in s:
			if not i.isdigit():
				l.append(t)
				l.append(i)
				t = ''
			else:
				t += i
		l.append(t)
		l = l[::-1]
		s = ''.join(l)
		return s


class Solution:

	def reverseEqn(self, s):
		st = []
		i = 0
		while i < len(s):
			ss = ''
			while i < len(s) and s[i] not in ['+', '-', '*', '/']:
				ss += s[i]
				i += 1
			st.append(ss)
			if i < len(s):
				st.append(s[i])
			i += 1
		st = st[::-1]
		sss = ''.join(st)
		return sss


class Stack:

	def __init__(self, maxsize):
		self.maxsize = maxsize
		self.top = -1
		self.arr = []

	def empty(self):
		return self.top == -1

	def full(self):
		return self.top == self.maxsize

	def push(self, data):
		if self.full():
			return 'Stack is full'
		self.top += 1
		self.arr.append(data)

	def peek(self):
		if self.empty():
			return 'Stack is empty'
		return self.arr[self.top]

	def pop(self):
		if self.empty():
			return 'Stack is empty'
		top_ele = self.arr.pop()
		self.top -= 1
		return top_ele

class Solution:

	def reverseEqn(self, s):
		stack = Stack(maxsize=len(s))
		num = ''
		for ele in s:
			if ele in ('+', '-', '*', '/'):
				if num != '':
					stack.push(num)
					stack.push(ele)
					num = ''
				else:
					stack.push(ele)
			else:
				num += ele
		stack.push(num)
		final = ''
		while not stack.empty():
			final += stack.pop()
		return final


class Solution:

	def reverseEqn(self, s):
		o = ['+', '-', '*', '/']
		o1 = []
		t = []
		s1 = ''
		for i in s:
			if i in o:
				o1.append(i)
				t.append(s1)
				s1 = ''
			else:
				s1 += i
		t.append(s1)
		s1 = ''
		while len(t) > 0:
			if o1:
				s1 += t.pop() + o1.pop()
			else:
				s1 += t.pop()
		return s1


class Solution:

	def reverseEqn(self, s):
		sta = []
		ste = []
		st = ''
		for i in range(len(s)):
			if s[i] not in '+-/*':
				st += s[i]
			else:
				sta.append(st)
				st = ''
				ste.append(s[i])
			if len(s) - 1 == i:
				sta.append(st)
				st = ''
		st = sta.pop()
		n = len(ste)
		for i in range(n):
			st += ste.pop() + sta.pop()
		return st


class Solution:

	def reverseEqn(self, s):
		c = ''
		d = ''
		for i in s:
			if i.isdigit():
				c += i
			else:
				d = i + c + d
				c = ''
		return c + d


class Solution:

	def reverseEqn(self, s):
		res = ''
		stack = ''
		for i in s:
			if i.isdigit():
				stack += i
			else:
				res = i + stack + res
				stack = ''
		return stack + res


class Solution:

	def reverseEqn(self, s):
		str = ''
		stk = []
		start = 0
		found = False
		for i in range(len(s)):
			if ord(s[i]) >= 48 and ord(s[i]) <= 57:
				if found == True:
					continue
				start = i
				found = True
			elif found == True:
				stk.append(s[start:i])
				found = False
			if found == False:
				stk.append(s[i])
		if start > 0:
			stk.append(s[start:len(s)])
		while len(stk) != 0:
			str += stk.pop()
		return str


class Solution:

	def reverseEqn(self, s):
		s = s[::-1]
		(x, y) = ('', '')
		for i in s:
			if i in '*/+-':
				y = y + x + i
				x = ''
			else:
				x = i + x
		y += x
		return y


class Solution:

	def reverseEqn(self, s):
		ans = ''
		i = len(s) - 1
		l = ['*', '/', '-', '+']
		while i >= 0:
			if s[i] in l:
				ans += s[i]
				i -= 1
			else:
				a = ''
				while i >= 0 and s[i] not in l:
					a += s[i]
					i -= 1
				a = a[::-1]
				ans += a
		return ans


class Solution:

	def reverseEqn(self, s):
		l = ''
		p = ''
		for i in s:
			if i.isdigit():
				l += i
			else:
				l += ' '
				p += i
		m = []
		k = ''
		j = 0
		p = p[::-1]
		for i in l:
			if i != ' ':
				k += i
			else:
				m.append(k)
				k = ''
		m.append(k)
		k = ''
		m = m[::-1]
		for i in m:
			k += i
			if len(p) > j:
				k += p[j]
				j = j + 1
		return k


class Solution:

	def reverseEqn(self, s):
		(result, stck) = (str(), str())
		for item in s:
			if item.isdigit():
				stck += item
			else:
				result = item + stck + result
				stck = str()
		return stck + result


class Solution:

	def reverseEqn(self, s):
		l = []
		s1 = ''
		for i in range(len(s)):
			if s[i].isdigit() or s[i].isalpha():
				s1 += s[i]
			else:
				l.append(s1)
				l.append(s[i])
				s1 = ''
		l.append(s1)
		l.reverse()
		return ''.join(l)


class Solution:

	def reverseEqn(self, s):
		string = '0123456789'
		d = dict()
		for ele in string:
			d[ele] = 0
		l = []
		z = []
		j = 0
		for i in range(len(s)):
			if s[i] not in d.keys():
				z.append(s[i])
				p = i
				l.append(s[j:i])
				j = i + 1
		l.append(s[p + 1:])
		ans = []
		o = z[::-1]
		rev = l[::-1]
		for i in range(len(o)):
			ans.append(rev[i])
			ans.append(o[i])
		ans.append(rev[len(rev) - 1])
		return ''.join(ans)


class Solution:

	def reverseEqn(self, s):
		number = ''
		stack = []
		for i in s:
			if i in '+-*/':
				stack.append(number)
				stack.append(i)
				number = ''
			else:
				number = number + i
		stack.append(number)
		ans = ''
		while len(stack) != 0:
			ans = ans + stack.pop()
		return ans


class Solution:

	def reverseEqn(self, s):
		rev = ''
		dig = ''
		for i in range(len(s)):
			if s[i].isdigit() == True:
				dig = dig + s[i]
			else:
				rev = s[i] + dig + rev
				dig = ''
		return dig + rev


class Solution:

	def reverseEqn(self, s):
		j = 0
		s1 = ''
		s2 = ''
		li = []
		while j < len(s):
			if s[j] != '+' and s[j] != '-' and (s[j] != '*') and (s[j] != '/'):
				s1 += s[j]
				j += 1
			else:
				li.append(s1)
				li.append(s[j])
				s1 = ''
				j += 1
		li.append(s1)
		for i in range(len(li) - 1, -1, -1):
			s2 += li[i]
		return s2


class Solution:

	def reverseEqn(self, s):
		st = []
		temp = ''
		for i in s:
			if i.isdigit():
				temp += i
			else:
				st.append(temp)
				st.append(i)
				temp = ''
		st.append(temp)
		st = st[::-1]
		return ''.join(st)


class Solution:

	def reverseEqn(self, s):
		stack = []
		r = ''
		m = ''
		for i in s:
			if i.isdigit() == True:
				r += i
			else:
				stack.append(r)
				r = ''
				if i.isdigit() == False:
					stack.append(i)
		stack.append(r)
		while len(stack) > 0:
			m += stack.pop()
		return m


def helper(s):
	stack = []
	s = s[::-1]
	text = ''
	for let in s:
		if let in '-+/*':
			stack.append(text[::-1])
			stack.append(let)
			text = ''
		else:
			text += let
	if text:
		stack.append(text[::-1])
	res = ''.join(stack)
	return res

class Solution:

	def reverseEqn(self, s):
		return helper(s)


class Solution:

	def reverseEqn(self, s):
		self.s = s
		a = '+-/*'
		low = 0
		l = []
		o = []
		for i in range(len(s)):
			h = i
			if s[i] in a:
				o.append(s[i])
				l.append(s[low:h])
				low = i + 1
		l.append(s[low:])
		k = ''
		l.reverse()
		o.reverse()
		for i in range(len(o)):
			k += l[i] + o[i]
		for j in range(i + 1, len(l)):
			k += l[j]
		return k


class Solution:

	def reverseEqn(self, s):
		stack = []
		for i in s:
			stack.append(i)
		s2 = ''
		s3 = []
		while len(stack) != 0:
			x = stack.pop()
			try:
				x = int(x)
				s3.append(x)
				continue
			except:
				while len(s3) != 0:
					s2 += str(s3.pop())
				s2 += x
		while len(s3) != 0:
			s2 += str(s3.pop())
		return s2


class Solution:

	def reverseEqn(self, s):
		parts = []
		operand = ''
		for ch in s:
			if ch in ['+', '-', '*', '/']:
				parts.append(operand)
				parts.append(ch)
				operand = ''
			else:
				operand += ch
		if operand:
			parts.append(operand)
		return ''.join(parts[::-1])


class Solution:

	def reverseEqn(self, s):
		res = []
		stack = []
		for i in s:
			if i != '+' and i != '-' and (i != '*') and (i != '/'):
				stack.append(i)
			else:
				while stack:
					res.append(stack.pop())
				res.append(i)
		while stack:
			res.append(stack.pop())
		return ''.join(res[::-1])


class Solution:

	def reverseEqn(self, s):
		k = ''
		a = ''
		i = 0
		while i < len(s):
			if s[i] == '+' or s[i] == '-' or s[i] == '*' or (s[i] == '/'):
				k = a + k
				a = ''
				k = s[i] + k
				i = i + 1
			elif i == len(s) - 1:
				a = a + s[i]
				k = a + k
				i = i + 1
			else:
				a = a + s[i]
				i = i + 1
		return k


class Solution:

	def reverseEqn(self, s):
		s1 = ''
		l = list(s)
		l1 = ['+', '-', '*', '/']
		s2 = ''
		s3 = ''
		while l:
			a = l.pop()
			if a in l1:
				s3 += s2[::-1]
				s3 += a
				s2 = ''
			else:
				s2 += a
		s3 += s2[::-1]
		return s3


class Solution:

	def reverseEqn(self, s):
		numb = ''
		stack = []
		for i in s:
			if i != '+' and i != '*' and (i != '-') and (i != '/'):
				numb += i
			else:
				stack.append(numb)
				stack.append(i)
				numb = ''
		stack.append(numb)
		res = ''
		while len(stack) != 0:
			res += stack.pop()
		return res


class Solution:

	def reverseEqn(self, s):
		l = '*/+-'
		p = len(s)
		t = ''
		b = ''
		for i in range(p):
			if s[i] not in l:
				t = t + s[i]
			else:
				t = t + ' '
			if s[i] in l:
				b = b + s[i]
		a = t.split()
		b = b[::-1]
		a = a[::-1]
		r = ''
		for i in range(len(b)):
			r = r + a[i]
			r = r + b[i]
		r = r + a[-1]
		return r


import re

class Solution:

	def reverseEqn(self, s):

		def push(stack, data):
			stack.insert(0, data)
		Numbers = re.split('\\D', s)
		Symbols = re.findall('\\D', s)
		Stack = []
		for (i, num) in enumerate(Numbers):
			if i < len(Numbers) - 1:
				push(Stack, num)
				push(Stack, Symbols[i])
			else:
				push(Stack, num)
		return ''.join(Stack)


import queue

class Solution:

	def reverseEqn(self, s):
		q = queue.LifoQueue()
		a = ''
		d = {'+': 1, '-': 1, '*': 2, '/': 2}
		for (i, v) in enumerate(s):
			if v in d.keys():
				q.put(a)
				q.put(v)
				a = ''
			elif i == len(s) - 1:
				a += v
				q.put(a)
			else:
				a += v
		res = ''
		while q.qsize():
			res += q.get()
		return res


class Solution:

	def reverseEqn(self, s):
		stack1 = []
		str1 = ''
		for i in s:
			if i == '*' or i == '-' or i == '+' or (i == '/'):
				s = s.replace(i, ' ')
				stack1.append(i)
		s = s.split()
		p = -1
		while len(stack1) and len(s):
			str1 = str1 + s[p] + stack1.pop()
			p = p - 1
		str1 = str1 + s[0]
		return str1


class Solution:

	def reverseEqn(self, s):
		stck = []
		j = ''
		i = 0
		while i < len(s):
			if s[i].isnumeric():
				j += s[i]
				i += 1
				if i == len(s):
					stck.append(int(j))
			elif len(j) > 0:
				stck.append(int(j))
				j = ''
			else:
				stck.append(s[i])
				i += 1
		q = ''
		for k in stck[::-1]:
			q += str(k)
		return q


class Solution:

	def reverseEqn(self, s):
		i = len(s) - 1
		a = ['*', '+', '-', '/']
		ans = ''
		while i >= 0:
			if s[i] not in a:
				m = ''
				while i >= 0:
					if s[i] in a:
						break
					m += s[i]
					i -= 1
				m = m[::-1]
				ans += m
			if i >= 0 and s[i] in a:
				ans += s[i]
				i -= 1
		return ans


class Solution:

	def reverseEqn(self, s):
		n = ''
		l = []
		ans = ''
		for i in range(0, len(s)):
			if s[i] >= '0' and s[i] <= '9':
				n = n + s[i]
			else:
				l.append(n)
				n = ''
				l.append(s[i])
		if n != 0:
			l.append(n)
		for i in range(len(l) - 1, -1, -1):
			ans = ans + l[i]
		return ans


class Solution:

	def reverseEqn(self, s):
		st = []
		n = len(s)
		i = 0
		ans = ''
		while i < n:
			if s[i] != '+' and s[i] != '-' and (s[i] != '*') and (s[i] != '/'):
				ans += s[i]
				i += 1
				if i == n:
					st.append(ans)
			elif len(ans) > 0:
				st.append(ans)
				ans = ''
			else:
				st.append(s[i])
				i += 1
		res = ''
		for i in st[::-1]:
			res += str(i)
		return res


class Solution:

	def reverseEqn(self, s):
		ans = ''
		idx = len(s) - 1
		while idx >= 0:
			if s[idx] in '+-*/':
				ans += s[idx]
				idx -= 1
			else:
				j = idx - 1
				while j >= 0 and s[j] not in '+-*/':
					j -= 1
				ans += s[j + 1:idx + 1]
				idx = j
		return ans


class Solution:

	def reverseEqn(self, s):
		(r, t) = ('', '')
		for e in s:
			if e.isdigit():
				t = t + e
			else:
				r = e + t + r
				t = ''
		return t + r


class Solution:

	def reverseEqn(self, s):
		self.stack = []
		self.string = ''
		for i in s:
			if i == '+' or i == '-' or i == '*' or (i == '/'):
				val = i
				s = s.replace(i, ' ')
				self.stack.append(val)
		s = s.split()
		p = -1
		k = len(self.stack)
		while k and len(s):
			self.string = self.string + s[p] + self.stack.pop()
			p = p - 1
			k = k - 1
		self.string = self.string + s[0]
		return self.string


class Solution:

	def reverseEqn(self, s):
		t = ''
		r = ''
		for i in s:
			if i.isnumeric():
				t = t + i
			else:
				r = i + t + r
				t = ''
		if t != '':
			r = t + r
		return r


class Solution:

	def reverseEqn(self, s):
		nums = []
		strg = ''
		for i in s:
			if i == '*' or i == '-' or i == '+' or (i == '/'):
				nums.append(strg)
				nums.append(i)
				strg = ''
			else:
				strg += i
		nums.append(strg)
		strg = ''
		for i in nums[::-1]:
			strg += i
		return strg


class Solution:

	def reverseEqn(self, s):
		l = []
		l1 = []
		symbols = ['+', '-', '*', '/']
		temp = ''
		for i in range(0, len(s)):
			if s[i] not in symbols:
				temp += s[i]
			else:
				l1.append(s[i])
				l.append(temp)
				temp = ''
		if len(temp) > 0:
			l.append(temp)
		ans = ''
		l = l[::-1]
		l1 = l1[::-1]
		for i in range(0, len(l)):
			ans += l[i]
			try:
				ans += l1[i]
			except:
				pass
		return ans


import re

class Solution:

	def reverseEqn(self, s):
		rev = ''
		s = re.split('(\\D)', s)
		return ''.join(s[::-1])


class Solution:

	def reverseEqn(self, s):
		ans = ''
		num = 0
		mul = 1
		nums = '0123456789'
		alpha = 'abcdefghijklmnopqrstuvwxyz'
		for i in range(len(s) - 1, -1, -1):
			if s[i] in alpha:
				ans += s[i]
			elif s[i] in nums:
				num += int(s[i]) * mul
				mul *= 10
			elif s[i + 1] in nums:
				ans += str(num)
				ans += s[i]
				mul = 1
				num = 0
			else:
				ans += s[i]
		if s[0] in nums:
			ans += str(num)
		elif s[0] in alpha:
			pass
		else:
			ans += s[0]
		return ans


class Solution:

	def reverseEqn(self, s):
		num = []
		oper = []
		operator = {'+', '-', '*', '/'}
		i = 0
		while i < len(s):
			t = ''
			while i < len(s) and s[i] not in operator:
				t += s[i]
				i += 1
			num.append(t)
			if i < len(s):
				oper.append(s[i])
			i += 1
		res = ''
		while oper:
			res += num.pop() + oper.pop()
		res += num.pop()
		return res


class Solution:

	def reverseEqn(self, s):
		str = []
		st = ''
		for i in s:
			if i == '*' or i == '/' or i == '+' or (i == '-'):
				str.append(st)
				st = i
				str.append(st)
				st = ''
			else:
				st += i
		str.append(st)
		st = ''
		for i in str[::-1]:
			st += i
		return st


class Solution:

	def reverseEqn(self, s):
		str = [] * len(s)
		c = []
		string = ''
		for i in range(len(s)):
			if s[i] == '+' or s[i] == '-' or s[i] == '*' or (s[i] == '/'):
				str.insert(0, string.join(c))
				c = []
				string = ''
				str.insert(0, s[i])
			else:
				c.append(s[i])
		if len(c) != 0:
			str.insert(0, string.join(c))
			string = ''
		return string.join(str)


class Solution:

	def reverseEqn(self, s):
		eqs = '+*-/'
		nums = []
		ops = []
		prev_op = -1
		n = len(s)
		for i in range(len(s)):
			if s[i] in eqs:
				ops.append(s[i])
				nums.append(s[prev_op + 1:i])
				prev_op = i
			elif i == n - 1:
				nums.append(s[prev_op + 1:])
		new_str = ''
		j = len(ops) - 1
		for i in nums[::-1]:
			new_str += i
			if j >= 0:
				new_str += ops[j]
				j -= 1
		return new_str


class Solution:

	def reverseEqn(self, s):
		ans = ''
		i = len(s) - 1
		while i >= 0:
			if s[i] != '+' and s[i] != '*' and (s[i] != '-') and (s[i] != '/'):
				j = i - 1
				while j > -1 and s[j] != '+' and (s[j] != '*') and (s[j] != '-') and (s[j] != '/'):
					j -= 1
				ans += s[j + 1:i + 1]
				i = j
			else:
				ans += s[i]
				i -= 1
		return ans


class Solution:

	def reverseEqn(self, s):
		stack = []
		S = ''
		for i in range(len(s)):
			if s[i] == '+' or s[i] == '-' or s[i] == '*' or (s[i] == '/'):
				stack.append(S)
				stack.append(s[i])
				S = ''
			else:
				S += s[i]
		stack.append(S)
		st = ''
		while stack:
			st += stack.pop()
		return st