Solve Sliding Window and Grid Search
Company: Bytedance
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates proficiency in string algorithms and constrained shortest-path/search problems, covering sliding-window techniques for bounded-distinct substrings and grid pathfinding with stateful fuel, recharge mechanics, and cost accumulation.
Part 1: Longest Substring With At Most K Distinct Characters
Constraints
- 0 <= len(s) <= 200000
- 0 <= k <= len(s)
- s may contain any printable characters
Examples
Input: ('eceba', 2)
Expected Output:
Explanation: The longest valid substring is 'ece', which has length 3.
Input: ('aa', 1)
Expected Output:
Explanation: The whole string contains only 1 distinct character.
Input: ('abaccc', 2)