Solve the following three coding tasks:
-
Two-pointer in-place de-duplication: Given a non-decreasing integer array nums and an integer k >= 1, modify nums in-place so that each distinct value appears at most k times. Return the new length L and ensure nums[0..L-1] holds the resulting sequence. Requirements: O(n) time, O(1) extra space. Prove correctness (loop invariants) and discuss off-by-one pitfalls. Test cases: [], k=1 -> L=0; [2,2,2], k=1 -> [2]; [1,1,1,2,2,3], k=2 -> prefix [1,1,2,2,3]; k >= n; array with many distinct values.
-
Sliding-window minimum cover: Given strings s and t, return the shortest substring of s that contains every character of t with multiplicity. If multiple, return the leftmost. Achieve O(|s| + |t|) time and O(Σ alphabet) space. Be Unicode-aware by code points; explain how you’d adapt if the alphabet is very large or if you must treat grapheme clusters. Example: s = "ADOBECODEBANC", t = "ABC" -> "BANC". Provide adversarial tests (e.g., repeated chars in t, no solution).
-
Non-LC style string processing: Implement sanitize(s) that (a) trims leading/trailing whitespace, (b) collapses any run of whitespace (spaces/tabs/newlines) into a single space, and (c) replaces every maximal run of ASCII digits [0-9]+ with a single token "#" while leaving all other characters unchanged. Do it in one pass O(n) and O(1) extra space (in-place if the language allows). Discuss pitfalls with multi-byte Unicode, combining marks, and surrogate pairs. Include unit tests that cover edge cases (empty string, only digits, mixed emoji, long whitespace runs).