To solve this problem efficiently, we'll maintain the string and keep track of the occurrences of the substring "1100". Since each update affects only a limited portion of the string, we can minimize the amount of work needed after each operation.
Here's a step-by-step approach:
-
Initial Setup:
- String Representation: We'll represent the string
s as a list for easier manipulation since strings in many programming languages are immutable.
- Tracking "1100" Occurrences: We'll keep a counter to track the number of times the substring
"1100" appears in s. Alternatively, we can store the starting indices of each occurrence in a set for quick lookups.
-
Processing Updates:
- Update Operation: For each operation, we're given an index
i and a value v. We'll update the i-th character of s to v.
- Affected Substrings: Changing a character can affect up to 4 substrings where the changed character is part of a potential
"1100" sequence. Specifically, the substrings starting at indices i-3, i-2, i-1, and i.
- Recheck Affected Substrings: For each of these starting positions, we'll check if the substring was
"1100" before the update and after the update. We'll adjust our counter or set accordingly.
-
Reporting the Result:
- After each update, we'll check if there's at least one occurrence of
"1100" in the string by verifying if our counter is greater than zero or if the set of starting indices is non-empty.
-
Edge Cases:
- Ensure that the indices checked do not go out of bounds, especially when
i is near the start or end of the string.
Below is a Python implementation of this approach:
def process_queries(s, queries):
s = list(s)
n = len(s)
count = 0
# Function to check if substring starting at idx is "1100"
def is_1100(idx):
if idx < 0 or idx + 3 >= n:
return False
return s[idx] == '1' and s[idx+1] == '1' and s[idx+2] == '0' and s[idx+3] == '0'
# Initialize count
for i in range(n - 3):
if is_1100(i):
count += 1
results = []
for (i, v) in queries:
i -= 1 # Convert to 0-based index
# Positions where "1100" could be affected: i-3 to i
affected_positions = range(i-3, i+1)
# Before update, check and decrement count if "1100" existed
for pos in affected_positions:
if is_1100(pos):
count -= 1
# Update the character
s[i] = v
# After update, check and increment count if "1100" exists
for pos in affected_positions:
if is_1100(pos):
count += 1
# Append result
results.append("YES" if count > 0 else "NO")
return results
# Example Usage:
# Initial string
s = "111000"
# Operations: list of tuples (i, v)
# For example, change position 3 to '0', position 4 to '1', etc.
queries = [
(3, '0'),
(4, '1'),
(2, '0'),
(5, '0'),
]
# Process queries
results = process_queries(s, queries)
# Output results
for res in results:
print(res)
Explanation of the Code:
-
Function is_1100(idx):
- Checks if the substring starting at index
idx is "1100".
- Returns
True if it is, False otherwise.
-
Initialization:
- Converts the string
s into a list for easier updates.
- Initializes the
count by iterating through the string and counting all occurrences of "1100".
-
Processing Each Query:
- For each query
(i, v):
- Converts the 1-based index
i to a 0-based index.
- Identifies all positions (
i-3 to i) that could be affected by the update.
- For each affected position, if
"1100" was present before the update, decrements the count.
- Updates the
i-th character to v.
- Again, for each affected position, if
"1100" is present after the update, increments the count.
- Appends
"YES" to the results if count > 0, otherwise "NO".
-
Example Usage:
- Demonstrates how to use the
process_queries function with an initial string and a series of update operations.
- Prints out
"YES" or "NO" after each operation based on whether "1100" is present in the string.
Time Complexity:
- Initialization: O(n), where n is the length of the string.
- Each Query: O(1), since we check a constant number of positions (up to 4) regardless of the string size.
- Overall: O(n + q), where q is the number of queries.
This approach ensures that even for large strings and a high number of queries, the solution remains efficient.