A string is a valid parentheses string (denoted VPS) if it meets one of the following:
" ", or a single character not equal to "( " or ") ",AB (A concatenated with B), where A and B are VPS's, or(A), where A is a VPS.We can similarly define the nesting depth depth(S) of any VPS S as follows:
depth( " ") = 0depth(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.
Example 1:
Input: s = "(1+(2*3)+((8)/4))+1 " Output: 3 Explanation: Digit 8 is inside of 3 nested parentheses in the string.
Example 2:
Input: s = "(1)+((2))+(((3))) " Output: 3
Constraints:
1 <= s.length <= 100s consists of digits 0-9 and characters '+', '-', '*', '/', '(', and ')'.s is a VPS.program main
! Solves the problem:
! 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 "( ".
! - It can be written as AB (A concatenated with B), where A and B are VPS's.
! - It can be written as (A), where A is a VPS.
implicit none
integer :: i, j, k, n, depth
character(len=100) :: s
! Examples
print '("Example 1:")'
s = "(1+(2*3)+((8)/4))+1"
print '(*(g0,1x))', s, depth(s)
print '("Example 2:")'
s = "(1)+((2))+(((3)))"
print '(*(g0,1x))', s, depth(s)
contains
function depth(s) result(d)
! Returns the nesting depth of a valid parentheses string (VPS)
implicit none
character(len=*), intent(in) :: s
integer :: d
d = 0
do i = 1, len(s)
select case(s(i:i))
case(' ')
cycle
case('(')
d = d + 1
case(')')
d = d - 1
end select
end do
end function depth
end program main
temp.f95:22:19:
22 | function depth(s) result(d)
| 1
Error: Procedure ‘depth’ at (1) has an explicit interface from a previous declaration
temp.f95:24:21:
24 | implicit none
| 1
Error: Unexpected IMPLICIT NONE statement in CONTAINS section at (1)
temp.f95:25:41:
25 | character(len=*), intent(in) :: s
| 1
Error: Unexpected data declaration statement in CONTAINS section at (1)
temp.f95:26:20:
26 | integer :: d
| 1
Error: Unexpected data declaration statement in CONTAINS section at (1)
temp.f95:28:13:
28 | d = 0
| 1
Error: Unexpected assignment statement in CONTAINS section at (1)
temp.f95:29:24:
29 | do i = 1, len(s)
| 1
Error: Unexpected DO statement in CONTAINS section at (1)
temp.f95:30:31:
30 | select case(s(i:i))
| 1
Error: Unexpected SELECT CASE statement in CONTAINS section at (1)
temp.f95:31:21:
31 | case(' ')
| 1
Error: Unexpected CASE statement at (1)
temp.f95:32:25:
32 | cycle
| 1
Error: CYCLE statement at (1) is not within a construct
temp.f95:33:21:
33 | case('(')
| 1
Error: Unexpected CASE statement at (1)
temp.f95:34:29:
34 | d = d + 1
| 1
Error: Unexpected assignment statement in CONTAINS section at (1)
temp.f95:35:21:
35 | case(')')
| 1
Error: Unexpected CASE statement at (1)
temp.f95:36:29:
36 | d = d - 1
| 1
Error: Unexpected assignment statement in CONTAINS section at (1)
temp.f95:37:15:
37 | end select
| 1
Error: Expecting END PROGRAM statement at (1)
temp.f95:38:11:
38 | end do
| 1
Error: Expecting END PROGRAM statement at (1)
temp.f95:39:7:
39 | end function depth
| 1
Error: Expecting END PROGRAM statement at (1)
module ParenthesesDepth
implicit none
private
public :: depth
contains
function depth(s) result(depth_)
character(len=*), intent(in) :: s
integer :: depth_
integer :: i, j, k
character :: c
logical :: is_open_paren
! Initialize variables
depth_ = 0
i = 1
j = 1
k = 1
is_open_paren = .false.
! Iterate over the characters in the string
do while (i <= len(s))
c = s(i:i)
! If we encounter an opening parenthesis, increment the nesting depth
if (c == '(') then
depth_ = depth_ + 1
is_open_paren = .true.
! If we encounter a closing parenthesis, decrement the nesting depth
else if (c == ')') then
depth_ = depth_ - 1
is_open_paren = .false.
end if
! If we encounter a digit and we are not in a nested expression,
! increment the index
if (c >= '0' .and. c <= '9' .and. .not. is_open_paren) then
j = j + 1
end if
! Increment the index
i = i + 1
end do
! Return the nesting depth
depth_ = depth_ + 1
end function depth
end module ParenthesesDepth
! Test the function with some examples
program test_depth
use ParenthesesDepth
implicit none
integer :: depth
! Test case 1: " "
depth = depth(" ")
write (*,*) "Test case 1: ", depth
! Test case 2: "()() "
depth = depth("()() ")
write (*,*) "Test case 2: ", depth
! Test case 3: "()(()()) "
depth = depth("()(()()) ")
write (*,*) "Test case 3: ", depth
! Test case 4: ")("
depth = depth(")(")
write (*,*) "Test case 4: ", depth
! Test case 5: "(() "
depth = depth("(() ")
write (*,*) "Test case 5: ", depth
! Test case 6: "(()())"
depth = depth("(()())")
write (*,*) "Test case 6: ", depth
! Test case 7: "(((((18)))))"
depth = depth("(((((18)))))")
write (*,*) "Test case 7: ", depth
! Test case 8: "(1+(2\*3)+((8)/4))+1 "
depth = depth("(1+(2*3)+((8)/4))+1 ")
write (*,*) "Test case 8: ", depth
! Test case 9: "(1)+((2))+(((3))) "
depth = depth("(1)+((2))+(((3))) ")
write (*,*) "Test case 9: ", depth
end program test_depth
temp.f95:53:20:
51 | use ParenthesesDepth
| 2
52 | implicit none
53 | integer :: depth
| 1
Error: Symbol ‘depth’ at (1) conflicts with symbol from module ‘parenthesesdepth’, use-associated at (2)
temp.f95:56:10:
56 | depth = depth(" ")
| 1
Error: ‘depth’ at (1) is not a variable
temp.f95:57:38:
57 | write (*,*) "Test case 1: ", depth
| 1
Error: Function ‘depth’ requires an argument list at (1)
temp.f95:60:10:
60 | depth = depth("()() ")
| 1
Error: ‘depth’ at (1) is not a variable
temp.f95:61:38:
61 | write (*,*) "Test case 2: ", depth
| 1
Error: Function ‘depth’ requires an argument list at (1)
temp.f95:64:10:
64 | depth = depth("()(()()) ")
| 1
Error: ‘depth’ at (1) is not a variable
temp.f95:65:38:
65 | write (*,*) "Test case 3: ", depth
| 1
Error: Function ‘depth’ requires an argument list at (1)
temp.f95:68:10:
68 | depth = depth(")(")
| 1
Error: ‘depth’ at (1) is not a variable
temp.f95:69:38:
69 | write (*,*) "Test case 4: ", depth
| 1
Error: Function ‘depth’ requires an argument list at (1)
temp.f95:72:10:
72 | depth = depth("(() ")
| 1
Error: ‘depth’ at (1) is not a variable
temp.f95:73:38:
73 | write (*,*) "Test case 5: ", depth
| 1
Error: Function ‘depth’ requires an argument list at (1)
temp.f95:76:10:
76 | depth = depth("(()())")
| 1
Error: ‘depth’ at (1) is not a variable
temp.f95:77:38:
77 | write (*,*) "Test case 6: ", depth
| 1
Error: Function ‘depth’ requires an argument list at (1)
temp.f95:80:10:
80 | depth = depth("(((((18)))))")
| 1
Error: ‘depth’ at (1) is not a variable
temp.f95:81:38:
81 | write (*,*) "Test case 7: ", depth
| 1
Error: Function ‘depth’ requires an argument list at (1)
temp.f95:84:10:
84 | depth = depth("(1+(2*3)+((8)/4))+1 ")
| 1
Error: ‘depth’ at (1) is not a variable
temp.f95:85:38:
85 | write (*,*) "Test case 8: ", depth
| 1
Error: Function ‘depth’ requires an argument list at (1)
temp.f95:88:10:
88 | depth = depth("(1)+((2))+(((3))) ")
| 1
Error: ‘depth’ at (1) is not a variable
temp.f95:89:38:
89 | write (*,*) "Test case 9: ", depth
| 1
Error: Function ‘depth’ requires an argument list at (1)
def maxDepth(s: str) -> int:
depth = 0
max_depth = 0
for c in s:
if c == '(':
depth += 1
max_depth = max(depth, max_depth)
elif c == ')':
depth -= 1
return max_depth
Iterate through the string s. When an opening parenthesis '(' is encountered, increment the depth value, update the max_depth with the maximum between the current max_depth and the depth. In contrast, when a closing parenthesis ')' is encountered, decrement the depth. The max_depth will be the nesting depth of the given valid parentheses string.
int maxDepth(std::string s) {
int depth = 0, max_depth = 0;
for (char c : s) {
if (c == '(') {
depth++;
max_depth = std::max(depth, max_depth);
} else if (c == ')') {
depth--;
}
}
return max_depth;
}
Iterate through the string s. When an opening parenthesis '(' is encountered, increment the depth value, update the max_depth with the maximum between the current max_depth and the depth. In contrast, when a closing parenthesis ')' is encountered, decrement the depth. The max_depth will be the nesting depth of the given valid parentheses string.