KatInterview

Shared-Letter Splits and Best Uniform Bundles

StringsAuto-graded · 24 test cases
MediumAuto-gradedStringsstringshash-mapcounting

Last updated

Count string split points whose halves share more than k distinct characters, and find the most equal-total bundles of one or two items from a cost list, with a special rule for zero-cost items.

You are given a string categories, an integer k, and a list of integers costs. Solve two independent tasks and return both answers together.

Task 1: Splits with many shared characters

A split at position i (where 1 <= i <= len(categories) - 1) cuts the string into a prefix categories[:i] and a suffix categories[i:]. Both parts are non-empty.

A split is good if the number of distinct characters that appear in both the prefix and the suffix is strictly greater than k.

Count the good splits. If the string has fewer than 2 characters, there are no splits and the count is 0.

Task 2: Largest set of equal-total bundles

You may form bundles from the items in costs. Each bundle holds one or two items, and its total is the sum of the costs of its items. Each item can be placed in at most one bundle, and items need not all be used.

All bundles you form must have the same total. You choose that total. Find the largest number of bundles you can form. If costs is empty, the answer is 0.

Items are distinguished by position, so two items with equal cost are still two separate items.

Rule for the total 0

When the chosen total is 0, items of cost 0 follow this rule: they are paired with each other first, as many pairs as possible. If one zero-cost item is left over, it forms a one-item bundle. So z zero-cost items yield exactly z // 2 + z % 2 bundles of total 0 (that is, ceil(z / 2)). Items with non-zero cost are not affected by this rule. Each of them can still go alone (only if its cost equals the total) or be paired with an item of the opposite cost.

For any non-zero total, a zero-cost item may be paired with an item whose cost equals the total, or left unused. Pairing it never beats using that item alone.

Output

Return a list [good_splits, max_bundles].

Constraints

  • categories is a string of characters. It may be empty or have length 1.
  • k is an integer and may be negative or larger than the number of distinct characters.
  • costs is a list of integers and may be empty. Values may be 0 or negative.
  • Assume the input sizes are moderate, for example up to a few thousand characters or items.
  • A bundle contains at least one and at most two items.

Examples

Example 1

Input: split_and_bundle('abaca', 1, [1, 2, 3, 4])

Output: [0, 2]

Example 2

Input: split_and_bundle('aaaa', 0, [2, 2, 2, 2])

Output: [3, 4]

Example 3

Input: split_and_bundle('abcde', 0, [1, 1, 2, 2, 3])

Output: [0, 3]

Example 4

Input: split_and_bundle('abab', 1, [5, 5, 5, 10, 0])

Output: [1, 3]

Hints

Hint 1

For Task 1, do not rebuild two sets at every split. Moving the split one step right transfers exactly one character from the suffix to the prefix, so only that character's status can change.

Hint 2

Track how many times each character still occurs in the suffix, plus the set of characters already seen in the prefix. Maintain a running count of characters that are in both, and adjust it in O(1) per step.

Hint 3

For Task 2, fix the target total T. Items equal to T can be bundled alone, and the other items pair up by complement (a with T - a), with each complement class independent. Score every candidate T at once by looping over pairs of distinct non-zero values, and treat zero-cost items at T = 0 with the special rule (ceil(z / 2) bundles).