NewThe Learning module is now liveSee what's new

You are given a string binaryString consisting only of the characters '0' and '1', together with an array requests describing operations to apply to it. Each entry of requests is one of two forms:

  • "count:<index>" returns the number of '1's in binaryString from position 0 through the given 0-based <index>, inclusive.
  • "flip" flips every character of binaryString in place: every '0' becomes '1' and every '1' becomes '0'.

Requests are processed from left to right. Every "flip" permanently updates the string before any later request observes it, so subsequent "count" requests are evaluated against the current (possibly flipped) string.

Return an array answers, where answers[k] is the answer to the kthk^{\text{th}} "count" request in the order the "count" requests appear. "flip" requests do not contribute an entry to answers.

Constraints:

  • 1 ≤ binaryString.length ≤ 105
  • binaryString[i] ∈ {'0', '1'}
  • 1 ≤ requests.length ≤ 105
  • For each "count:<index>" request: 0 ≤ <index> < binaryString.length

Example 1:

Input: binaryString = "0000101", requests = ["count:4", "count:6", "flip", "count:4", "flip", "count:2"] Output: [1, 2, 4, 0] Explanation:

  • "count:4": the current string is "0000101", and its prefix through index 4 is "00001", which contains one '1'. The answer is 1.
  • "count:6": the current string is still "0000101", and its prefix through index 6 is the full string "0000101", which contains two '1's. The answer is 2.
  • "flip": the string becomes "1111010".
  • "count:4": the prefix of "1111010" through index 4 is "11110", which contains four '1's.
  • "flip": the string becomes "0000101" again.
  • "count:2": the prefix through index 2 is "000", which contains zero '1's.

Example 2:

Input: binaryString = "1100110", requests = ["count:6", "flip", "flip", "count:6", "flip", "count:6"] Output: [4, 4, 3]

Example 3:

Input: binaryString = "10110100", requests = ["count:0", "count:3", "flip", "count:3", "count:7", "flip", "count:7"] Output: [1, 3, 1, 4, 4]


Accepted
14/39
Acceptance Rate35.9%
Test Cases
Input
binaryString =
requests =
Output
Expected