Find Every Position Where a Scrambled Hashtag Appears in a Caption
Last updated: November 29, 2026
Quick Overview
Given a caption string s and a hashtag body p, return all start indices in s where some permutation of p appears as a contiguous substring.
Meta
November 29, 20260
0
0 solved
Given a caption string s and a hashtag body p, return all start indices in s where some permutation of p appears as a contiguous substring.
A fixed-length sliding window with character counts, framed as catching scrambled spam hashtags on Instagram. The interviewer checks that you update counts in O(1) per slide instead of recounting each window.
What the Interviewer Expects
- Recognizes the window length is fixed at len(p)
- Maintains a count difference and a counter of mismatched characters
- Updates in O(1) per step for the entering and leaving characters
- Returns all matches including overlapping ones
- Gives O(n) time with O(alphabet) space
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- Return only whether any permutation exists (the boolean version).
- Allow at most one character to differ in each window.
- Support a large Unicode alphabet; does the approach change?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Input: strings s and p, any characters, lengths up to 10^5. Output: a sorted list of indices i where s[i:i+len(p)] is an anagram of p. If p is longer than s, return []....
High-Level Design
Keep need[c] = count in p minus count in the current window, and bad = number of characters whose need is nonzero. Initially the window is empty, so bad is the number of distinct characters in...