You are given a binary string s and a list of queries. Process the queries in order. Each query is one of two types:
-
"count"
: report the number of
'1'
characters in the current string.
-
"flip"
: invert every bit, so each
'0'
becomes
'1'
and each
'1'
becomes
'0'
.
Return a list with the answers to the "count" queries, in the order those queries appear.
Function Signature
def flip_and_count(s: str, queries: list[str]) -> list[int]:
Rules
-
A
"flip"
query changes the string for every later query.
-
If there are no
"count"
queries, return an empty list.
Constraints
-
1 <= len(s) <= 10^5
-
1 <= len(queries) <= 10^5
-
Each
queries[i]
is either
"count"
or
"flip"
.
-
s
consists only of
'0'
and
'1'
.
Examples
Input: s = "10110", queries = ["count", "flip", "count", "flip", "count"]
Output: [3, 2, 3]
The string starts with 3 ones. After the first flip it is "01001", which has 2 ones. The second flip restores "10110", so the last count is 3 again.
Input: s = "0000", queries = ["count", "flip", "count"]
Output: [0, 4]
The string has no ones at first. After the flip it is "1111", which has 4.
Input: s = "1", queries = ["flip", "flip", "flip", "count"]
Output: [0]
Three flips leave the string inverted as "0", so the only count is 0.