The title
A string is called a valid parentheses string (VPS) if it satisfies one of the following conditions:
A string is an empty string “”, or a single character that is not “(” or “)”. The string can be written AB (A concatenated with B strings), where A and B are valid parenthesis strings. The string can be written as (A), where A is A valid parenthesis string. Similarly, we can define the nesting depth(S) of any valid parenthesis string S:
Depth (“”) = 0 depth(C) = 0, where C is A single character string and the character is not “(” or “)” depth(A + B) = Max (depth(A), depth(B)), Depth (“(” + A + “)”) = 1 + depth(A) = 1 + depth(A) “”,” () () “, “() () () ()” is a valid string brackets (nested depth of 0, 1, 2, respectively), while “) (“, “(()” is not effective brackets string.
Given a valid parenthesis string s, returns the string’s nesting depth of s.
Example 1: Input: s = "(1+(2*3)+((8)/4))+1" Output: 3 Explanation: The number 8 is in nested 3-layer brackets. Example 2: input: s = "(1) + ((2)) + (((3)))" output: 3 example 3: input: s = "1 + (2 * 3)/(2-1)" output: 1 example 4: input: s = "1" output: 0Copy the code
Tip:
1 <= s.length <= 100 s consists of the numbers 0-9 and the characters ‘+’, ‘-‘, ‘*’, ‘/’, ‘(‘, ‘)’
Their thinking
Class Solution: def maxDepth(self, s: STR) -> int: maxRet = 0 tempCount = 0 for I in s: if I == "(": tempCount += 1 elif i == ")": if maxRet < tempCount: maxRet = tempCount tempCount -= 1 return maxRet if __name__ == '__main__': s = "(1+(2*3)+((8)/4))+1" ret = Solution().maxDepth(s) print(ret)Copy the code