An automated trading bot ingests a sequence of n tick signals encoded as a string s of length n. Each character is one of three kinds: B means the bot is forced to buy exactly one unit that tick; S means it is forced to sell exactly one unit that tick; ? means the tick is ambiguous, and you get to decide -- independently for every ? -- whether it counts as a buy or a sell, in order to maximize the magnitude of the bot's net position (total buys minus total sells) once all n ticks have been resolved. Determine the largest possible value of |net position| achievable by choosing the best interpretation for every ?.
n -- the number of ticks.s of length n, consisting only of the characters B, S, and ?.A single integer: the maximum achievable |net position|.
s has length exactly n and consists only of the characters B, S, ?.Example 1
Input
5 BS??B
Expected
3
Explanation
There are 2 forced buys (positions 0 and 4), 1 forced sell (position 1), and 2 ambiguous ticks (positions 2 and 3). The base imbalance is |2-1|=1 in favor of buys, so pushing both ambiguous ticks toward buy adds 2 more, giving a maximum net position of 1+2=3.
Example 2
Input
4 ????
Expected
4
Explanation
There are no forced ticks, so the base imbalance is 0. All 4 ambiguous ticks can be resolved the same way (e.g. all as buys), giving a maximum net position of 0+4=4.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →