KatInterview

Remainder-Sum Windows and Bracket Cleanup

Arrays & HashingAuto-graded · 23 test cases
MediumAuto-gradedArrays & Hashingprefix-summodular-arithmetichash-counting

Last updated

Count contiguous subarrays whose sum has a given remainder using prefix-sum remainders, and delete the fewest parentheses to balance a string in a fixed, deterministic way.

Implement one function that solves two independent subtasks and returns both answers together.

Part A: Subarrays with a given remainder

You get a list of integers values, a modulus modulus, and a target remainder target. Count the contiguous, non-empty subarrays whose sum, taken modulo modulus, equals target.

  • Subarrays are distinguished by their position, so two equal subarrays at different positions are counted separately.
  • The remainder is always in the range 0 to modulus - 1, even for negative sums. For example, with modulus = 3, a sum of -1 has remainder 2.

Part B: Minimal bracket cleanup

You get a string text made of lowercase letters, ( and ). Delete as few parentheses as possible so that the remaining parentheses are balanced, without reordering any characters. Letters are never deleted.

A string is balanced when, in every prefix, the number of ( is at least the number of ), and the totals over the whole string are equal.

Several minimal answers can exist, so to make the output unique, build it exactly like this:

  1. Scan left to right while tracking the number of currently open (. Drop every ) that appears when that number is 0. Keep everything else.
  2. If some ( remain unmatched after this scan, say e of them, remove the last e occurrences of ( in the string produced by step 1.

Return the resulting string.

Return value

Return a list of two items: [count, cleaned_text], where count is the integer from Part A and cleaned_text is the string from Part B.

Constraints

  • 0 <= len(values) <= 10^5
  • Each value is an integer that fits in a normal 64-bit signed integer; it may be negative or zero.
  • 1 <= modulus <= 10^4
  • 0 <= target < modulus
  • text contains only lowercase letters, ( and ). It may be empty.
  • Aim for O(n + modulus) time for Part A and linear time for Part B.

Examples

Example 1

Input: count_remainder_windows_and_fix_brackets([1, 1, 1], 3, 0, 'lee(t(c)o)de')

Output: [1, 'lee(t(c)o)de']

Example 2

Input: count_remainder_windows_and_fix_brackets([2, -1, 2], 3, 1, 'a)b(c)d')

Output: [2, 'ab(c)d']

Example 3

Input: count_remainder_windows_and_fix_brackets([], 1, 0, ')((')

Output: [0, '']

Hints

Hint 1

Write each subarray sum as a difference of two prefix sums. The condition on the remainder then becomes a condition on the remainders of two prefix sums.

Hint 2

While scanning, keep a count array indexed by prefix remainder. The prefix remainder you look up for the current prefix is (current - target) mod modulus. Don't forget the empty prefix, whose remainder is 0.

Hint 3

For the parentheses, first drop every `)` that has no open `(` before it. Any `(` still unmatched are then removed from the right end, which keeps all earlier prefixes valid.