Given a string S, the task is to find the bracket numbers. 
Example 1:
Input:  S = "(aa(bdc))p(dee)"
Output: 1 2 2 1 3 3
Explanation: The highlighted brackets in
the given string (aa(bdc))p(dee) has been 
assigned the numbers as: 1 2 2 1 3 3.
Example 2:
Input:  S = "(((()("
Output: 1 2 3 4 4 5
Explanation: The highlighted brackets in
the given string (((()( has been assigned
the numbers as: 1 2 3 4 4 5
User Task:
Your task is to complete the function bracketNumbers() which takes a single string as input and returns a list of numbers. You do not need to take any input or print anything.
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(|S|)
Constraints:
1 <= |S| <= 10^{5}
S contains lower case English alphabets, and '(', ')' characters
At any time the number of opening brackets are greater than closing brackets

Source: geeksforgeeks | Difficulty: EASY
Tags: String algorithms

Data structures

Starter code: #User function Template for python3

class Solution:

	def bracketNumbers(self, S):

		# code here

Solutions:
class Solution:

	def bracketNumbers(self, S):
		ans = []
		stack = []
		(x, y) = (0, 0)
		for c in S:
			if c == '(':
				x += 1
				stack.append(x)
				ans.append(x)
			elif c == ')':
				ans.append(stack.pop())
		return ans


class Solution:

	def bracketNumbers(self, s):
		l = []
		k = 0
		stack = []
		for i in s:
			if i == '(':
				k += 1
				l.append(k)
				stack.append(k)
			if i == ')':
				l.append(stack.pop())
		return l


class Solution:

	def bracketNumbers(self, S):
		st = []
		ans = []
		c = 0
		for i in S:
			if i == '(':
				c += 1
				st.append(c)
				ans.append(c)
			elif i == ')':
				ans.append(st.pop())
		return ans


class Solution:

	def bracketNumbers(self, S):
		result = []
		count = 0
		stack = []
		for item in S:
			if item == '(':
				count += 1
				result.append(count)
				stack.append(count)
			if item == ')':
				result.append(stack.pop())
		return result


class Solution:

	def bracketNumbers(self, S):
		stack = []
		count = 0
		result = []
		n = len(S)
		for i in range(n):
			if S[i] == '(':
				count += 1
				result.append(count)
				stack.append(count)
			if S[i] == ')':
				result.append(stack.pop())
		return result


class Solution:

	def bracketNumbers(self, s):
		stack = []
		num = 0
		ans = []
		for i in range(len(s)):
			if s[i] == '(':
				num = num + 1
				stack.append([s[i], num])
				ans.append(stack[-1][1])
			elif s[i] == ')':
				ans.append(stack[-1][1])
				stack.pop()
		return ans


class Solution:

	def bracketNumbers(self, S):
		l = []
		c = 0
		stack = []
		for i in S:
			if i == '(':
				c += 1
				l.append(c)
				stack.append(c)
			if i == ')':
				l.append(stack.pop())
		return l


class Solution:

	def bracketNumbers(self, S):
		s = []
		xl = []
		x = 0
		for i in S:
			if i == '(':
				x = x + 1
				s.append(x)
				xl.append(x)
			elif i == ')':
				xl.append(s.pop())
		return xl


class Solution:

	def bracketNumbers(self, S):
		li = []
		stack = []
		a = 0
		for i in S:
			if i == '(':
				a += 1
				li.append(a)
				stack.append(a)
			if i == ')':
				li.append(stack.pop())
		return li


class Solution:

	def bracketNumbers(self, S):
		count = 1
		ans = []
		st = []
		for i in range(len(S)):
			if S[i] == '(':
				st.append((S[i], count))
				ans.append(count)
				count += 1
			elif S[i] == ')':
				(_, k) = st.pop()
				ans.append(k)
		return ans


class Solution:

	def bracketNumbers(self, S):
		li = []
		li1 = []
		count = 0
		for i in range(len(S)):
			if S[i] == '(':
				count += 1
				li.append(count)
				li1.append(li[-1])
			elif S[i] == ')':
				li1.append(li[-1])
				li.pop()
		return li1


class Solution:

	def bracketNumbers(self, S):
		r = 0
		l = []
		stk = []
		for i in S:
			if i == '(':
				r += 1
				l.append(r)
				stk.append(r)
			elif i == ')':
				l.append(stk.pop())
		return l


class Solution:

	def bracketNumbers(self, S):
		stack = []
		ans = []
		opening = 0
		for ch in S:
			if ch == '(':
				opening += 1
				stack.append([ch, opening])
				ans.append(opening)
			if ch == ')':
				ans.append(stack[-1][1])
				stack.pop()
		return ans


class Solution:

	def bracketNumbers(self, S):
		n = len(S)
		st = list()
		res = []
		ori = 1
		for i in range(n):
			if S[i] == '(':
				st.append(ori)
				res.append(ori)
				ori += 1
			elif S[i] == ')':
				res.append(st[-1])
				st.pop()
		return res


class Solution:

	def bracketNumbers(self, S):
		num = 1
		st = []
		res = []
		for i in S:
			if i == '(':
				st.append(num)
				res.append(num)
				num += 1
			elif i == ')':
				res.append(st.pop())
		return res


class Solution:

	def barcketNumbers(self, x):
		count = 1
		ans = []
		st = []
		for i in range(len(x)):
			if x[i] == '(':
				st.append((x[i], count))
				ans.append(count)
				count += 1
			elif x[i] == ')':
				(_, k) = st.pop()
				ans.append(k)
		return ans


class Solution:

	def barcketNumbers(self, S):
		res = []
		stack = []
		count = 0
		for i in S:
			if i == '(':
				count += 1
				stack.append(count)
				res.append(count)
			elif i == ')':
				res.append(stack[-1])
				stack.pop()
		return res


class Solution:

	def barcketNumbers(self, S):
		ans = list()
		left_bnum = 1
		right_bnum = list()
		for i in range(len(S)):
			if S[i] == '(':
				ans.append(left_bnum)
				right_bnum.append(left_bnum)
				left_bnum += 1
			elif S[i] == ')':
				ans.append(right_bnum[-1])
				right_bnum.pop()
		return ans


class Solution:

	def barcketNumbers(self, S):
		result = []
		stack = []
		c = 0
		for i in S:
			if i == '(':
				c += 1
				result.append(c)
				stack.append(c)
			if i == ')':
				result.append(stack.pop())
		return result


class Solution:

	def barcketNumbers(self, S):
		st = []
		j = 1
		lis = []
		for i in S:
			if st:
				if st and i == ')':
					lis.append(st[-1])
					st.pop()
				elif i == '(':
					st.append(j)
					lis.append(j)
					j += 1
			elif i == '(':
				st.append(j)
				lis.append(j)
				j += 1
		return lis


class Solution:

	def barcketNumbers(self, S):
		res = []
		s = []
		c = 1
		for i in S:
			if i == '(':
				s.append(c)
				res.append(c)
				c += 1
			elif i == ')':
				res.append(s.pop())
		return res


class Solution:

	def barcketNumbers(self, S):
		result = []
		count = 0
		stack = []
		for item in S:
			if item == '(':
				count += 1
				result.append(count)
				stack.append(count)
			if item == ')':
				if not stack:
					result.append(-1)
				else:
					result.append(stack.pop())
		return result


class Solution:

	def barcketNumbers(self, S):
		i = 1
		ans = []
		stack = []
		for s in S:
			if s == '(':
				ans.append(i)
				stack.append(i)
				i += 1
			elif s == ')':
				ans.append(stack[-1])
				stack.pop()
		return ans


class Solution:

	def barcketNumbers(self, S):
		x = []
		c = 0
		y = []
		for i in S:
			if i == '(':
				c += 1
				x.append(c)
				y.append(c)
			elif i == ')':
				x.append(y.pop())
		return x


class Solution:

	def barcketNumbers(self, S):
		c = 0
		r = []
		s = []
		for i in S:
			if i == '(':
				c += 1
				r.append(c)
				s.append(c)
			if i == ')':
				r.append(s.pop())
		return r


import re

class Solution:

	def barcketNumbers(self, S):
		result = []
		c = 0
		stack = []
		for item in S:
			if item == '(':
				c += 1
				result.append(c)
				stack.append(c)
			if item == ')':
				result.append(stack.pop())
		return result


class Solution:

	def barcketNumbers(self, S):
		t = []
		t1 = []
		t2 = []
		a = 0
		s1 = ''
		for i in S:
			if i == '(' or i == ')':
				t.append(i)
		for i in t:
			if i == '(':
				a += 1
				t1.append(a)
				t2.append(i)
				s1 += ' ' + str(a)
			if i == ')' and t2[-1] == '(':
				s1 += ' ' + str(t1.pop())
				t2.pop()
		return s1.split()


class Solution:

	def barcketNumbers(self, S):
		sta = []
		k = 1
		R = []
		for i in S:
			if i == '(':
				sta.append(k)
				R.append(k)
				k += 1
			elif i == ')':
				R.append(sta[-1])
				sta.pop()
		return R


class Solution:

	def barcketNumbers(self, S):
		cnt = 0
		st = []
		ans = []
		N = len(S)
		for i in range(N):
			if S[i] == '(':
				cnt += 1
				ans.append(cnt)
				st.append(cnt)
			elif S[i] == ')':
				ans.append(st.pop())
		return ans


class Solution:

	def barcketNumbers(self, S):
		list1 = []
		list2 = []
		count = 0
		for i in S:
			if i == '(':
				count = count + 1
				list1.append(count)
				list2.append(count)
			if i == ')':
				list1.append(list2.pop())
		return list1


class Solution:

	def barcketNumbers(self, S):
		l = list(S)
		res = []
		n = []
		no = 0
		o = 0
		for i in l:
			if i == '(':
				no += 1
				n.append(no)
				res.append(no)
			elif i == ')':
				res.append(n[-1])
				n.pop()
		return res


class Solution:

	def barcketNumbers(self, S):
		stax = []
		res = []
		c = 0
		for i in S:
			if i == '(':
				c += 1
				res.append(c)
				stax.append(c)
			if i == ')':
				res.append(stax.pop())
		return res


class Solution:

	def barcketNumbers(self, S):
		l = []
		c = 0
		c1 = []
		for i in S:
			if i == '(':
				c += 1
				l.append(c)
				c1.append(c)
			elif i == ')':
				l.append(c1.pop())
		return l


class Solution:

	def barcketNumbers(self, S):
		stack = []
		l = []
		c = 0
		for i in range(len(S)):
			if S[i] == '(':
				c += 1
				stack.append(c)
				l.append(stack[len(stack) - 1])
			elif S[i] == ')':
				l.append(stack.pop())
		return l


import re

class Solution:

	def barcketNumbers(self, S):
		openb = 0
		closeb = []
		ans = []
		for i in S:
			if i == '(':
				openb += 1
				closeb.append(openb)
				ans.append(openb)
			elif i == ')':
				ans.append(closeb[-1])
				closeb.pop(-1)
		return ans


class Solution:

	def barcketNumbers(self, S):
		(a, s) = ([], [])
		c = 0
		for i in range(len(S)):
			if S[i] == '(':
				c = c + 1
				a.append(c)
				s.append(c)
			elif S[i] == ')':
				s.append(a[-1])
				a.pop()
		return s


class Solution:

	def barcketNumbers(self, S):
		char_list = []
		open_stack = []
		open_count = 0
		close_count = 0
		for char in S:
			if char == '(':
				open_count = open_count + 1
				char_list.append(open_count)
				open_stack.append(open_count)
			elif char == ')':
				count = open_stack.pop()
				char_list.append(count)
		return char_list


class Solution:

	def barcketNumbers(self, S):
		answer = []
		st = []
		l = 1
		for i in range(len(S)):
			if S[i] == '(':
				st.append(l)
				answer.append(l)
				l += 1
			elif S[i] == ')':
				st.append(answer[-1])
				answer.pop()
		return st


class Solution:

	def barcketNumbers(self, s):
		c = 0
		st = []
		ans = []
		for i in s:
			if i == '(':
				c += 1
				st.append(c)
				ans.append(c)
			elif i == ')':
				ans.append(st.pop())
		return ans


class Solution:

	def barcketNumbers(self, s):
		stack = []
		res = []
		i = 0
		for elm in s:
			if elm == '(':
				i += 1
				stack.append(i)
				res.append(i)
			elif elm == ')':
				res.append(stack.pop())
		return res


class Solution:

	def barcketNumbers(self, S):
		stack = []
		l = []
		mydict = {'(': ')'}
		c = 0
		for i in S:
			if i in mydict:
				c = c + 1
				stack.append(c)
				l.append(c)
			elif i in mydict.values():
				l.append(stack.pop())
		return l


class Solution:

	def barcketNumbers(self, S):
		ans = []
		left = 1
		right = list()
		for i in range(len(S)):
			if S[i] == '(':
				ans.append(left)
				right.append(left)
				left += 1
			elif S[i] == ')':
				ans.append(right[-1])
				right.pop()
		return ans


class Solution:

	def barcketNumbers(self, S):
		ans = []
		stk = []
		count = 0
		for ch in S:
			if ch == '(':
				count += 1
				stk.append(count)
				ans.append(count)
			elif ch == ')':
				ans.append(stk.pop())
		return ans


class Solution:

	def barcketNumbers(self, S):
		openbr = 0
		ans = []
		stack = []
		for i in range(len(S)):
			if S[i] == '(':
				openbr += 1
				stack.append(openbr)
				ans.append(openbr)
			if S[i] == ')':
				ans.append(stack.pop())
		return ans


class Solution:

	def barcketNumbers(self, S):
		count = 0
		count1 = 0
		brack = []
		my_list = []
		for i in S:
			if i == '(':
				count1 = count1 + 1
				my_list.append(count1)
				brack.append(count1)
			elif i == ')':
				brack.append(my_list[-1])
				my_list.pop()
		return brack


class Solution:

	def barcketNumbers(self, S):
		n = len(S)
		count = 1
		list1 = []
		list2 = []
		for i in range(n):
			if S[i] == '(':
				list2.append(count)
				list1.append(count)
				count = count + 1
			elif S[i] == ')':
				list2.append(list1.pop())
		return list2


class Solution:

	def barcketNumbers(self, S):
		(stack, val) = ([], [])
		p = 0
		for i in S:
			if i == '(':
				p += 1
				stack.append([i, p])
				val.append(p)
			elif i == ')':
				cur = stack.pop()
				val.append(cur[1])
			else:
				continue
		return val


from collections import deque

class Solution:

	def barcketNumbers(self, s):
		stack = deque()
		l = []
		c = 0
		for i in range(len(s)):
			if s[i] == '(':
				c += 1
				stack.append(c)
				l.append(c)
			elif s[i] == ')':
				l.append(stack.pop())
		return l


class Solution:

	def barcketNumbers(self, S):
		stck = list()
		res = list()
		count = 0
		for i in S:
			if i == '(':
				count += 1
				res.append(count)
				stck.append(count)
			if i == ')':
				res.append(stck.pop())
		return res


class Solution:

	def barcketNumbers(self, S):
		openBracket = 0
		ourStack = []
		ourAnswer = []
		for i in S:
			if i == '(':
				openBracket += 1
				ourStack.append(openBracket)
				ourAnswer.append(openBracket)
			elif i == ')':
				ourAnswer.append(ourStack.pop())
		return ourAnswer


class Solution:

	def barcketNumbers(self, S):
		st = []
		brc = 0
		out = []
		for i in S:
			if i == '(':
				brc += 1
				out.append(brc)
				st.append(brc)
			elif i == ')':
				out.append(st.pop())
			else:
				pass
		return out


class Solution:

	def barcketNumbers(self, s):
		li = []
		o = 0
		li2 = []
		for i in s:
			if i == '(':
				o += 1
				li2.append(o)
				li.append(o)
			elif i == ')':
				li2.append(li[-1])
				if len(li) != 0:
					li.pop()
		return li2


class Solution:

	def barcketNumbers(self, S):
		x = []
		y = []
		count = 0
		for i in range(len(S)):
			if S[i] == '(':
				count += 1
				x.append(count)
				y.append(count)
			elif S[i] == ')':
				x.append(y.pop())
		return x


class Solution:

	def barcketNumbers(self, S):
		stack = []
		ans = []
		n = len(S)
		var = 0
		for i in range(n):
			if S[i] == '(':
				var += 1
				ans.append(var)
				stack.append(var)
			elif S[i] == ')':
				ans.append(stack.pop())
		return ans


class Solution:

	def barcketNumbers(self, S):
		count = 0
		stack = []
		lst = []
		for i in range(len(S)):
			if S[i] == '(':
				count += 1
				stack.append(count)
				lst.append(stack[-1])
			elif len(stack) != 0 and S[i] == ')':
				lst.append(stack[-1])
				stack.pop()
		return lst


class Solution:

	def barcketNumbers(self, S):
		Stack = []
		ans = []
		k = 0
		Stack = []
		for i in S:
			if i == '(':
				k += 1
				ans.append(k)
				Stack.append(k)
			elif i == ')' and len(Stack) != 0:
				ans.append(Stack.pop())
		return ans


class Solution:

	def barcketNumbers(self, S):
		a = list(S)
		l1 = []
		l2 = []
		(i, j) = (0, 0)
		L = len(a)
		for i in range(L):
			if a[i] == '(':
				j = j + 1
				l1.append(j)
				l2.append(j)
			elif a[i] == ')':
				l1.append(l2.pop())
		return l1


class Solution:

	def barcketNumbers(self, S):
		c = 0
		l = []
		l1 = []
		for i in S:
			if i == '(':
				c += 1
				l.append(c)
				l1.append(c)
			elif i == ')':
				if len(l1) >= 1:
					a = l1.pop()
					l.append(a)
		return l


class Solution:

	def barcketNumbers(self, S):
		st = []
		ans = []
		num = 0
		for i in S:
			if i == '(':
				num = num + 1
				st.append(num)
				ans.append(num)
			if i == ')':
				ans.append(st.pop())
		return ans


class Solution:

	def barcketNumbers(self, S):
		stack = []
		co = 0
		output = []
		for i in S:
			if i == '(':
				co += 1
				stack.append(co)
				output.append(co)
			elif i == ')':
				output.append(stack.pop())
		return output


class Solution:

	def barcketNumbers(self, S):
		List = []
		Stack = []
		i = 1
		for char in S:
			if char is '(':
				List.append(i)
				Stack.append(i)
				i += 1
			if char is ')':
				List.append(Stack.pop())
		return List