Minimum Deletions to Make Alternating Substring Problem
Description
LeetCode Problem 3777.
You are given a string s of length n consisting only of the characters 'A' and 'B'.
You are also given a 2D integer array queries of length q, where each queries[i] is one of the following:
[1, j]: Flip the character at indexjofsi.e.'A'changes to'B'(and vice versa). This operation mutatessand affects subsequent queries.[2, l, r]: Compute the minimum number of character deletions required to make the substrings[l..r]alternating. This operation does not modifys; the length ofsremainsn.
A substring is alternating if no two adjacent characters are equal. A substring of length 1 is always alternating.
Return an integer array answer, where answer[i] is the result of the i^th query of type [2, l, r].
Example 1:
Input: s = "ABA", queries = [[2,1,2],[1,1],[2,0,2]]
Output: [0,2]
Explanation:
i queries[i] j l r s before query s[l..r] Result Answer
0 [2, 1, 2] - 1 2 "ABA" "BA" Already alternating 0
1 [1, 1] 1 - - "ABA" - Flip s[1] from 'B' to 'A' -
2 [2, 0, 2] - 0 2 "AAA" "AAA" Delete any two 'A's to get "A" 2
Thus, the answer is [0, 2].
Example 2:
Input: s = "ABB", queries = [[2,0,2],[1,2],[2,0,2]]
Output: [1,0]
Explanation:
i queries[i] j l r s before query s[l..r] Result Answer
0 [2, 0, 2] - 0 2 "ABB" "ABB" Delete one 'B' to get "AB" 1
1 [1, 2] 2 - - "ABB" - Flip s[2] from 'B' to 'A' -
2 [2, 0, 2] - 0 2 "ABA" "ABA" Already alternating 0
Thus, the answer is [1, 0].
Example 3:
Input: s = "BABA", queries = [[2,0,3],[1,1],[2,1,3]]
Output: [0,1]
Explanation:
i queries[i] j l r s before query s[l..r] Result Answer
0 [2, 0, 3] - 0 3 "BABA" "BABA" Already alternating 0
1 [1, 1] 1 - - "BABA" - Flip s[1] from 'A' to 'B' -
2 [2, 1, 3] - 1 3 "BBBA" "BBA" Delete one 'B' to get "BA" 1
Thus, the answer is [0, 1].
Constraints:
1 <= n == s.length <= 10^5s[i]is either'A'or'B'.1 <= q == queries.length <= 10^5queries[i].length == 2or3queries[i] == [1, j]or,queries[i] == [2, l, r]0 <= j <= n - 10 <= l <= r <= n - 1
Solution
Approach: Fenwick Tree
Mark position i with 1 when s[i] == s[i-1] (and 0 otherwise, always 0 for i = 0). Making s[l..r] alternating needs one deletion for each adjacent equal pair inside it, so the answer is the sum of the marks over [l+1, r].
Keep the marks in a Fenwick tree (binary indexed tree). Flipping s[j] toggles the mark at j and the mark at j+1 (if it exists), which are two point updates. A query is the difference of two prefix sums.
Complexity: time O((n + q) log n), space O(n), where n is the length of s and q is the number of queries.
class BinaryIndexedTree:
__slots__ = "n", "c"
def __init__(self, n: int):
self.n = n
self.c = [0] * (n + 1)
def update(self, x: int, delta: int) -> None:
while x <= self.n:
self.c[x] += delta
x += x & -x
def query(self, x: int) -> int:
s = 0
while x:
s += self.c[x]
x -= x & -x
return s
class Solution:
def minDeletions(self, s: str, queries: List[List[int]]) -> List[int]:
n = len(s)
nums = [0] * n
bit = BinaryIndexedTree(n)
for i in range(1, n):
nums[i] = int(s[i] == s[i - 1])
if nums[i]:
bit.update(i + 1, 1)
ans = []
for q in queries:
if q[0] == 1:
j = q[1]
delta = (nums[j] ^ 1) - nums[j]
nums[j] ^= 1
bit.update(j + 1, delta)
if j + 1 < n:
delta = (nums[j + 1] ^ 1) - nums[j + 1]
nums[j + 1] ^= 1
bit.update(j + 2, delta)
else:
_, l, r = q
ans.append(bit.query(r + 1) - bit.query(l + 1))
return ans