Badge Scanned Twice Too Soon
Easy · Sliding Window · about 12 min
An office door logs badge scans in order: badges[i] is the badge id of scan i. Security wants to know if the same badge was scanned twice within k scans, that is, at positions i < j with j - i <= k.
Return True if that happens anywhere, otherwise False. k is 0 or more.
Examples:
rescan_within(["ana", "ben", "cal", "ana"], 3) → True (positions 0 and 3)
rescan_within(["ana", "ben", "cal", "ana"], 2) → False
Hints
- Keep a map from each badge to the position where you saw it last.
- For each scan, check the gap to that last position, then update it to the current position.
- A gap of exactly k counts.
Learn the pattern first: the Sliding Window lesson.
More Sliding Window problems
- Food Stall Moving Average (Easy)
- Longest Stretch With No Repeat Song (Medium)
- Repaint Tiles for the Longest Run (Medium)
- Scrambled Code Hidden in a Log (Medium)
- Fewest Orders to Hit the Goal (Medium)
- Shortest Note That Covers Every Letter (Hard)
- Rolling Peak of a Sensor (Hard)