diff --git a/CB.EN.U4CSE16234_lab2_q10.py b/CB.EN.U4CSE16234_lab2_q10.py new file mode 100644 index 0000000000000000000000000000000000000000..4d05881d5eeb555fb1f0a6cacfca54f04e6d5d22 --- /dev/null +++ b/CB.EN.U4CSE16234_lab2_q10.py @@ -0,0 +1,26 @@ +def maxDepth(S): + current_max = 0 + max = 0 + n = len(S) + + # Traverse the input string + for i in xrange(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 + + # finally check for unbalanced string + if current_max != 0: + return -1 + + return max + +s=input("Enter to check") +print maxDepth(s) \ No newline at end of file