String Parentheses : 4April POTD

String Parentheses : 4April POTD

Day 9/75:

Question: Maximum Nesting Depth of the Parentheses -(Easy)

A string is a valid parentheses string (denoted VPS) if it meets one of the following:

  • It is an empty string "", or a single character not equal 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 not equal to "(" or ")".
  • 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 "()(()())" are 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.

Input: s = "(1+(2*3)+((8)/4))+1"
Output: 3
Explanation: Digit 8 is inside of 3 nested parentheses in the string.
        
Input: s = "(1)+((2))+(((3)))"
Output: 3
        

?Intuition: Count of maximum open braces is required (max depth)

Approach1: Stack

Stack is the first approach which basically comes ,when we see Parentheses.

Simply traverse the string and push the Open Brace '(' into stack ,calculate max size of stack thereby poping when encounters Closing Brace '('

Return maximum size of stack.

Code : (Stack)

Time Complexity: O(n)

Space Complexity : O(n) - Stack is used.


==========================================

Approach 2: Optimized

  1. Initialize count to track braces count and res to find max count of open braces.
  2. If char == ' ( ' , increment count and update res with max count.
  3. When char ==' ) ' , decrement count.
  4. Return res, with maximum count of Open Parentheses.

Code:

Time Complexity: O(n)

Space Complexity : O(1) - No extra space is used.


Related Question : Maximum Nesting Depth of Two Valid Parentheses Strings


要查看或添加评论,请登录

Suyash Awathe的更多文章

  • Tree: Add One Row to Tree

    Tree: Add One Row to Tree

    Day 17: Problem Statement: Given the root of a binary tree and two integers val and depth, add a row of nodes with…

  • Monotonic Array / TwoPointer :Trapping Rain Water: Most Asked !!

    Monotonic Array / TwoPointer :Trapping Rain Water: Most Asked !!

    Day 16/75 : Problem : Trapping Rain Water -(Hard) Given n non-negative integers representing an elevation map where the…

  • Stack: April POTD

    Stack: April POTD

    Question: Remove K Digits -(Medium) Given string num representing a non-negative integer num, and an integer k, return…

  • Queue-Array: April POTD

    Queue-Array: April POTD

    Day 14/75: Question: Reveal Cards In Increasing Order -(Medium) You are given an integer array . There is a deck of…

    1 条评论
  • Array: 9 April POTD

    Array: 9 April POTD

    Day 13/75: Question: Time Needed to Buy Tickets -(Easy) There are people in a line queuing to buy tickets, where the…

  • Queue : April POTD

    Queue : April POTD

    Day 12/75: Question: Number of Students Unable to Eat Lunch -(Easy) The school cafeteria offers circular and square…

    1 条评论
  • String : Parentheses : April-POTD

    String : Parentheses : April-POTD

    Day 11/75: Question: Minimum Remove to Make Valid Parentheses -(Medium) Given a string s of , and lowercase English…

    3 条评论
  • String : 5April POTD

    String : 5April POTD

    Day 10/75: Question: Make The String Great -(Easy) Given a string of lower and upper case English letters. A good…

  • BackTracking : WordSearch :4April POTD

    BackTracking : WordSearch :4April POTD

    Day 9/75: Question: Word Search -(Medium) Given an m x n grid of characters board and a string word, return true if…

    1 条评论
  • Isomorphic String : April POTD

    Isomorphic String : April POTD

    Day 8/75: Question: Given two strings s and t, determine if they are isomorphic. Two strings and are isomorphic if the…

    2 条评论

社区洞察

其他会员也浏览了