Coding · should-tier · asked at Amazon, Apple, Google, Meta, Microsoft, Netflix
Permutation in String
Problem
Given two lowercase strings s1 and s2, determine whether some contiguous substring of s2 is a rearrangement of s1, that is, whether s2 contains a substring with exactly the same letters, with the same multiplicities, as s1.
- Each string has between 1 and 20,000 characters.
- Only lowercase
a-zappears in either string.
Examples
s1 = "gh", s2 = "nlghdd"→True. The substring"gh"appears directly.s1 = "world", s2 = "dlrrowoowl"→False. Every five-letter window is missing at least one of the required letters or has a duplicate.s1 = "xyz", s2 = "zyx"→True. All ofs2is a rearrangement ofs1.
Hints
- Every candidate substring has the same length as
s1. What does that fix about the window? - Two strings are rearrangements of each other exactly when their letter counts agree. Can you update a count table as the window slides by one?
- Fixed-size sliding window over
s2with a 26-entry count array; maintain how many of the 26 letters currently matchs1's count, so each step is O(1).
Last reviewed 8 September 2026
Locked
Approach · Solution (Python) · Complexity · Follow-ups · Company variations · Tests
The rest of this question — the worked approach, the Python solution with passing tests, complexity, follow-ups and how each company frames it — is part of the guides.
Get the full guide
Pick the company you're interviewing with. Each guide has every question tiered for that loop — one-time purchase, lifetime access.
Secure payment by Stripe · instant access · refund questions: [email protected]
Also in: Apple, Google, Meta, Microsoft, Netflix
Interviewing at more than one? All 6 guides for $499 · save $395
Not ready to buy? Get the free starter pack — 13 questions with full solutions.