You have an integer variable ( x ) which is initially set to 0. You are provided with a string ( S ) of length ( N ) that consists of the characters 'I' and 'D'. You will perform the following operations based on the characters in the string:
For each character ( S[i] ):
If ( S[i] = 'I' ), increment the value of ( x ) by 1.
If ( S[i] = 'D' ), decrement the value of ( x ) by 1.
Your task is to determine the maximum value taken by ( x ) during these operations, which includes the value of ( x ) before the first operation and after the last operation.
Input Format
A single string ( S ) consisting of characters 'I' and 'D'. The length of the string ( N ) will be in the range ( 1 \leq N \leq 10^6 ).
Output Format
Return a single integer: the maximum value taken by ( x ) throughout the operations.
Examples
Example 1
Input:
text
S = "IIDDI"
Output:
text
2
Explanation:
Initial value: ( x = 0 )
After operations: ( I \rightarrow x = 1 ), ( I \rightarrow x = 2 ), ( D \rightarrow x = 1 ), ( D \rightarrow x = 0 ), ( I \rightarrow x = 1 ).
The maximum value of ( x ) during these operations is 2.
Example 2
Input:
text
S = "DIDIDI"
Output:
text
1
Explanation:
Initial value: ( x = 0 )
After operations: ( D \rightarrow x = -1 ), ( I \rightarrow x = 0 ), ( D \rightarrow x = -1 ), ( I \rightarrow x = 0 ), ( D \rightarrow x = -1 ), ( I \rightarrow x = 0 ).
The maximum value of ( x ) during these operations is 1.
Constraints
The input string will only contain the characters 'I' and 'D'.
The length of ( S ) will not exceed ( 10^6 ).
Solution
Please implement your solution in the /app/ directory.