
You are required to change exactly N commands in the string. Changing 'S' to 'R' or 'R' to 'S' both count as one change. After making exactly N changes, you execute the new sequence of commands.
Your task is to find the maximum possible absolute displacement from the starting point (position 0). The displacement is the absolute value of your final position.
The first line of input contains a string Str, representing the sequence of commands.
The second line contains an integer N, representing the exact number of commands that must be changed.
Print a single integer representing the maximum absolute displacement achievable.
To achieve the maximum positive displacement, you should prioritize changing 'R's to 'S's. If you run out of 'R's to change but still have changes left, you'll be forced to make suboptimal changes ('S' to 'R'). A similar logic applies to achieving the maximum negative displacement. The final answer is the larger of these two possible absolute outcomes.
SRSR
1
2
The string is "SRSR" (2 'S', 2 'R'). We must make N=1 change.
To maximize positive displacement: Change one 'R' to an 'S'. The new string could be "SSSR". The final position is 3 - 1 = 2.
To maximize negative displacement: Change one 'S' to an 'R'. The new string could be "RRSR". The final position is 1 - 3 = -2.
The absolute displacements are |2| = 2 and |-2| = 2. The maximum is 2.
S
1
1
The string is "S" (1 'S', 0 'R'). We must make N=1 change. The only possible change is 'S' to 'R'.
The new string is "R". The final position is -1.
The maximum absolute displacement is |-1| = 1.
The expected time complexity is O(N).
1 <= length of Str <= 10^5
0 <= N <= length of Str
Time limit: 1 sec