From 113d6041d052db1442b7880c09c23567d44f28c9 Mon Sep 17 00:00:00 2001 From: Pravesh <cb.en.u4cse16234@cb.students.amrita.edu> Date: Sun, 6 Jan 2019 15:25:33 +0530 Subject: [PATCH] lab2 eval --- CB.EN.U4CSE16234_lab2_q10.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 CB.EN.U4CSE16234_lab2_q10.py diff --git a/CB.EN.U4CSE16234_lab2_q10.py b/CB.EN.U4CSE16234_lab2_q10.py new file mode 100644 index 0000000..4d05881 --- /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 -- GitLab