A string is a valid parentheses string (denoted VPS ) if is meets any one of the following:
	It is an empty string "", or a single character not equals to " ( "  or  " ) " ,
	It can be written as AB (A concatenated with B ),
	where A and B are VPS's , or
	It can be written as (A) , where A is a VPS.
We can similarly define the nesting depth depth (S) of any VPS S as follows:
	depth ("") = 0
	depth (C) = 0, where C is a string with a single character excluding "(" and ")" ,
	depth (A + B) = max (depth (A) , depth (B)) , where A and B are VPS's.
	depth ( "(" + A + ")" ) = 1 + depth (A) , where A is a VPS.
For example , "" , " ( ) ( ) " , and " ( ) ( ( ) ( ) ) " and VPS's (with nesting depths 0 , 1, and 2), and " ) ( " and " ( ( ) "   are not VPS's.
Given a VPS represented as string S , return the nesting depth of S.
 
Example 1:
Input: s = " ((5+2)(3+4)((6))) "
Output: 3
Explanation: Character '6' is inside three nested parentheses.
 
Example 2:
Input: " (43+4++3)((3)(9))+1 "
Output: 2
Explanation: Character '3' and '9' both are inside two nested parentheses.
 
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxDepth() which takes the string s as input parameters and returns nesting depth of the given string s .
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(1)
Constraints:
1<=|s|<=100
s consists of digits 0-9 and characters ' + ' , ' - ' , ' * ' , ' / ' , ' ( ' , and ' ) '.
It is guaranteed that parentheses expression s is a VPS.

Source: geeksforgeeks | Difficulty: EASY
Tags: String algorithms

Data structures

Starter code: #User function Template for python3



class Solution:

    def maxDepth(self, s):

        # Code here

Solutions:
class Solution:

	def maxDepth(self, s):
		count = 0
		res = 0
		for i in s:
			if i == '(':
				count += 1
				res = max(count, res)
			elif i == ')':
				count -= 1
		return res


class Solution:

	def maxDepth(self, s):
		i = 0
		n = len(s)
		maxi = 0
		opened = 0
		while i < n:
			if s[i] == '(':
				opened += 1
				maxi = max(maxi, opened)
			elif s[i] == ')':
				opened -= 1
			i += 1
		return maxi


class Solution:

	def maxDepth(self, s):
		ma = 0
		c = 0
		for i in range(len(s)):
			if s[i] == '(':
				c += 1
				ma = max(ma, c)
			elif s[i] == ')':
				c -= 1
		return ma


class Solution:

	def maxDepth(self, s):
		depth = 0
		for i in range(len(s)):
			leftParentheses = s[:i + 1].count('(')
			rightParentheses = s[:i + 1].count(')')
			depth = max(depth, leftParentheses - rightParentheses)
		return depth


class Solution:

	def maxDepth(self, s):
		count = 0
		maxm = -1
		for char in s:
			if char == '(':
				count += 1
			elif char == ')':
				count -= 1
			if count > maxm:
				maxm = count
		return maxm


class Solution:

	def maxDepth(self, s):
		stack = []
		maxdepth = 0
		for i in s:
			if i == '(':
				stack.append('(')
				maxdepth = max(maxdepth, len(stack))
			if i == ')':
				stack.pop()
		return maxdepth


class Solution:

	def maxDepth(self, s):
		ans = 0
		par_ans = 0
		for el in s:
			if el == '(':
				par_ans += 1
			elif el == ')':
				par_ans -= 1
			ans = max(ans, par_ans)
		return ans


class Solution:

	def maxDepth(self, s):
		mx = 0
		open = 0
		for i in range(len(s)):
			if s[i] == '(':
				open += 1
			elif s[i] == ')':
				open -= 1
			mx = max(mx, open)
		return mx


class Solution:

	def maxDepth(self, s):
		c = 0
		lst = []
		for i in s:
			if i == '(':
				lst.append(i)
				c = max(c, len(lst))
			elif i == ')':
				lst.pop()
		return c


class Solution:

	def maxDepth(self, s):
		d = 0
		max = 0
		for i in s:
			if i == '(':
				d = d + 1
			elif i == ')':
				d = d - 1
			if max < d:
				max = d
		return max


class Solution:

	def maxDepth(self, s):
		count = 0
		max_count = 0
		for i in range(len(s) - 1):
			if s[i] == s[i + 1]:
				count = count + 1
				max_count = max(max_count, count)
			elif s[i] != s[i + 1]:
				count = 0
		if max_count >= 1:
			return max_count + 1
		return max_count


class Solution:

	def maxDepth(self, s):
		overall_max = 0
		current_max = 0
		for i in range(len(s)):
			if s[i] == '(':
				current_max += 1
				overall_max = max(current_max, overall_max)
			elif s[i] == ')':
				current_max -= 1
		if current_max != 0:
			return -1
		return overall_max


from collections import deque

class Solution:

	def maxDepth(self, s):
		stack = deque()
		maxi = 0
		for char in s:
			if char == '(':
				stack.append(char)
				maxi = max(maxi, len(stack))
			elif char == ')' and stack[-1] == '(':
				stack.pop()
		return maxi


class Solution:

	def maxDepth(self, s):
		openbracket = 0
		ans = 0
		n = len(s)
		for i in range(n):
			if s[i] == '(':
				openbracket += 1
				ans = max(ans, openbracket)
			elif s[i] == ')':
				openbracket -= 1
		return ans


class Solution:

	def maxDepth(self, s: str) -> int:
		current_depth = 0
		max_depth = 0
		for char in s:
			if char == '(':
				current_depth += 1
				if current_depth > max_depth:
					max_depth = current_depth
			elif char == ')':
				current_depth -= 1
		return max_depth


class Solution:

	def maxDepth(self, s):
		count = 0
		maxcount = 0
		for ele in s:
			if ele == '(':
				count += 1
				maxcount = max(maxcount, count)
			elif ele == ')':
				count -= 1
		return maxcount


class Solution:

	def maxDepth(self, s):
		co = 0
		max_d = 0
		for i in s:
			if i == '(':
				co += 1
				max_d = max(max_d, co)
			elif i == ')':
				co -= 1
		return max_d


class Solution:

	def maxDepth(self, s):
		max_depth = 0
		curr_depth = 0
		for elem in s:
			if elem == '(':
				curr_depth = curr_depth + 1
				max_depth = max(max_depth, curr_depth)
			elif elem == ')':
				curr_depth = curr_depth - 1
		return max_depth


class Solution:

	def maxDepth(self, s):
		res = cur = 0
		for c in s:
			if c == '(':
				cur += 1
				res = max(res, cur)
			if c == ')':
				cur -= 1
		return res


class Solution:

	def maxDepth(self, s):
		(size, c, res) = (len(s), 0, -1)
		for i in s:
			if i == '(':
				c += 1
			elif i == ')':
				c -= 1
			res = max(res, c)
		return res


class Solution:

	def maxDepth(self, s):
		max_depth = 0
		cur_depth = 0
		for c in s:
			if c == '(':
				cur_depth += 1
				max_depth = max(max_depth, cur_depth)
			elif c == ')':
				cur_depth -= 1
			else:
				pass
		return max_depth


class Solution:

	def maxDepth(self, s):
		ans = 0
		for char in s:
			if char == '(':
				count = 0
			elif char == ')':
				count += 1
				ans = max(count, ans)
		return ans


class Solution:

	def maxDepth(self, s):
		n = len(s)
		stk = []
		res = 0
		for i in range(n):
			if s[i] == '(':
				stk.append('(')
				res = max(res, len(stk))
			elif s[i] == ')':
				if stk and stk[-1] == '(':
					stk.pop()
		return res


class Solution:

	def maxDepth(self, s):
		stk = []
		maxi = float('-inf')
		for i in s:
			if i == '(' or i == ')':
				stk.append(i)
		if len(stk) == 0:
			return 0
		stk2 = []
		stk2.append(stk[0])
		for i in stk[1:]:
			maxi = max(maxi, len(stk2))
			if i == ')' and len(stk2) > 0:
				if stk2[-1] == '(' and i == ')' or (stk2[-1] == ')' and i == '('):
					stk2.pop()
			else:
				stk2.append(i)
		return maxi


class Solution:

	def maxDepth(self, s):
		stack = []
		count = 0
		max_count = 0
		for char in s:
			if char != '(' and char != ')':
				continue
			if char == '(':
				stack.append('(')
				count += 1
			else:
				max_count = max(count, max_count)
				count -= 1
				stack.pop()
		return max_count


class Solution:

	def maxDepth(self, s):
		c = 0
		depth = 0
		for i in s:
			if i == '(':
				c += 1
				depth = max(c, depth)
			elif i == ')':
				c -= 1
		return depth


class Solution:

	def maxDepth(self, s):
		count = 0
		max_num = 0
		for i in s:
			if i == '(':
				count += 1
				if max_num < count:
					max_num = count
			if i == ')':
				count -= 1
		return max_num


class Solution:

	def maxDepth(self, s):
		count = 0
		maxi = 0
		for item in s:
			count += item == '('
			count -= item == ')'
			maxi = max(maxi, count)
		return maxi


class Solution:

	def maxDepth(self, s):
		d = 0
		l = []
		for i in s:
			if i == '(':
				l.append(i)
			if i == ')':
				l.pop()
			d = max(d, len(l))
		return d


class Solution:

	def maxDepth(self, s):
		mx = 0
		k = 0
		for i in s:
			if i == '(':
				k += 1
			elif i == ')':
				k -= 1
			mx = max(mx, k)
		return mx


class Solution:

	def maxDepth(self, s):
		st = []
		d = 0
		for i in s:
			if i == '(':
				st.append(i)
			elif i == ')':
				st.pop()
			else:
				pass
			d = max(d, len(st))
		return d


class Solution:

	def maxDepth(self, s):
		arr = []
		ans = 0
		for i in range(len(s)):
			if s[i] == '(':
				arr.append(s[i])
			elif s[i] == ')':
				if len(arr) > 0:
					ans = max(ans, len(arr))
					arr.pop()
		return ans


class Solution:

	def maxDepth(self, s):
		count = 0
		x = 0
		for el in s:
			if el == '(':
				count += 1
				if count > x:
					x = count
			elif el == ')':
				count -= 1
		return x


class Solution:

	def maxDepth(self, s):
		x = []
		p = 0
		m = 0
		for i in s:
			if i == '(':
				p += 1
			elif i == ')':
				m = max(m, p)
				p -= 1
		return m


class Solution:

	def maxDepth(self, s):
		count = 0
		maxi = 0
		for i in s:
			if i == '(':
				count += 1
				maxi = max(maxi, count)
			if i == ')':
				count -= 1
		return maxi


class Solution:

	def maxDepth(self, S):
		current_max = 0
		max = 0
		n = len(S)
		for i in range(n):
			if S[i] == '(':
				current_max += 1
				if current_max > max:
					max = current_max
			elif S[i] == ')':
				if current_max > 0:
					current_max -= 1
				else:
					return -1
		if current_max != 0:
			return -1
		return max


class Solution:

	def maxDepth(self, s):
		m = 0
		n = len(s)
		c = 0
		for i in s:
			if i == '(':
				c += 1
				m = max(c, m)
			elif i == ')':
				c -= 1
		return m


class Solution:

	def maxDepth(self, s):
		max_depth = 0
		cnt = 0
		for c in s:
			if c == '(':
				cnt += 1
				if cnt > max_depth:
					max_depth = cnt
			elif c == ')':
				cnt -= 1
		return max_depth


class Solution:

	def maxDepth(self, s):
		maxD = 0
		curD = 0
		for x in s:
			if x == '(':
				curD += 1
			elif x == ')':
				curD -= 1
			maxD = max(maxD, curD)
		return maxD


class Solution:

	def maxDepth(self, s):
		stack = []
		maxheight = 0
		for i in s:
			if i == '(':
				stack.append(i)
				if len(stack) > maxheight:
					maxheight = len(stack)
			if i == ')':
				stack.pop()
		return maxheight


class Solution:

	def maxDepth(self, s):
		depth = 0
		i = count = 0
		while i < len(s):
			if s[i] == '(':
				count += 1
			elif s[i] == ')':
				count -= 1
			depth = max(depth, count)
			i += 1
		return depth


class Solution:

	def maxDepth(self, s):
		count = 0
		x = 0
		for element in s:
			if element == '(':
				count += 1
				if count > x:
					x = count
			elif element == ')':
				count -= 1
			else:
				pass
		return x


class Solution:

	def maxDepth(self, s):
		depth = 0
		op = 0
		for p in s:
			if p == '(':
				op += 1
			if p == ')':
				op -= 1
			depth = max(depth, op)
		return depth


class Solution:

	def maxDepth(self, s):
		maximum = []
		count = 0
		for i in s:
			if i == '(':
				count += 1
			elif i == ')':
				count -= 1
				maximum.append(count)
		return max(maximum) + 1


class Solution:

	def maxDepth(self, s):
		c = 0
		l = []
		for i in range(len(s)):
			if s[i] == '(':
				l.append(i)
			elif s[i] == ')':
				if c < len(l):
					c = len(l)
				l.pop()
		if c == 0:
			return -1
		else:
			return c


import re

class Solution:

	def maxDepth(self, s):
		a = []
		ans = 0
		for i in range(len(s)):
			if s[i] == '(':
				a.append('(')
				ans = max(ans, len(a))
			elif s[i] == ')':
				a.pop()
		return ans


class Solution:

	def maxDepth(self, s):
		cnt = 0
		max_depth = 0
		for char in s:
			if char not in ['(', ')']:
				continue
			elif char == '(':
				cnt += 1
			elif char == ')':
				cnt -= 1
			max_depth = max(max_depth, cnt)
		return max_depth


class Solution:

	def maxDepth(self, s):
		stack = []
		count = 0
		l = []
		for i in s:
			if not stack or i == '(':
				stack.append(i)
				count = 0
			elif stack and i == ')':
				stack.pop()
				count += 1
			l.append(count)
		return max(l)


class Solution:

	def maxDepth(self, s):
		st = []
		res = 0
		for i in s:
			if i == '(':
				st.append(i)
				res = max(res, len(st))
			elif i == ')':
				st.pop()
		return res


class Solution:

	def maxDepth(self, s):
		maxx = 0
		ch = 0
		for i in s:
			if i == '(':
				ch += 1
			elif i == ')':
				ch -= 1
			if ch > maxx:
				maxx = ch
		return maxx


class Solution:

	def maxDepth(self, s):
		op = 0
		ans = 0
		for ch in s:
			if ch == '(':
				op += 1
				ans = max(ans, op)
			if ch == ')':
				op -= 1
		return ans


class Solution:

	def maxDepth(self, s):
		op = 0
		ans = 0
		for i in s:
			if i == '(':
				op += 1
				ans = max(ans, op)
			elif i == ')':
				op -= 1
		if op == 0:
			return ans
		else:
			ans = 0
			return ans


class Solution:

	def maxDepth(self, s):
		mx = 0
		depth = 0
		for x in s:
			if x == '(':
				depth += 1
				mx = max(mx, depth)
			elif x == ')':
				depth -= 1
		return mx


class Solution:

	def maxDepth(self, s):
		depth = 0
		max_depth = 0
		for char in s:
			if char == '(':
				depth += 1
				max_depth = max(depth, max_depth)
			elif char == ')':
				depth -= 1
		return max_depth


class Solution:

	def maxDepth(self, s):
		c = 0
		a = 0
		for i in s:
			if i == '(':
				c += 1
				if c > a:
					a = c
			elif i == ')':
				c -= 1
		return a


class Solution:

	def maxDepth(self, s):
		md = 0
		cd = 0
		for i in range(len(s)):
			if s[i] == '(':
				cd += 1
				md = max(md, cd)
			if s[i] == ')':
				cd -= 1
		return md


class Solution:

	def maxDepth(self, s):
		c = 0
		st1 = []
		for i in range(len(s)):
			if s[i] == '(':
				st1.append(i)
			elif s[i] == ')':
				if c < len(st1):
					c = len(st1)
				st1.pop()
		return c


class Solution:

	def maxDepth(self, s):
		open = ['(']
		closed = [')']
		cnt = 0
		maximum = 0
		for i in s:
			if i in open:
				cnt += 1
			elif i in closed:
				maximum = max(maximum, cnt)
				cnt -= 1
		return maximum


class Solution:

	def maxDepth(self, s):
		out = 0
		p = 0
		for i in s:
			if i == '(':
				p += 1
				out = max(out, p)
			elif i == ')':
				p -= 1
		return out


class Solution:

	def maxDepth(self, s):
		l = list()
		ans = 0
		for x in s:
			if x == '(':
				l.append('(')
				ans = max(ans, len(l))
			elif x == ')':
				l.pop()
		return ans


class Solution:

	def maxDepth(self, s):
		a = max = 0
		for i in s:
			if i == '(':
				a += 1
				max = a if a > max else max
			elif i == ')':
				a -= 1
		return max


class Solution:

	def maxDepth(self, s):
		depth = 0
		open1 = 0
		for c in s:
			if c == '(':
				open1 += 1
			if c == ')':
				open1 -= 1
			depth = max(depth, open1)
		return depth


class Solution:

	def maxDepth(self, s):
		maxD = 0
		stack = []
		for i in s:
			if i == '(':
				stack.append(i)
				if maxD < len(stack):
					maxD = len(stack)
			elif i == ')':
				stack.pop(0)
		return maxD