Permutation in String
Python data engineering interview problem. Difficulty: intermediate. Pattern: Sliding Window. About 14 minutes. Part of the Pro drill bank.
Whether s2 contains any permutation of s1 as a substring. Treat this as a production helper: match the contracted return shape, including empty and duplicate inputs.
A message may contain a short pattern in any order, such as a shuffled pair of required letters. You need to know whether one such permutation appears as a contiguous part of a larger string. Return True if s2 contains a substring whose character counts exactly match s1. The order inside that window may differ, because any permutation of s1 is accepted. Write the function check_inclusion(s1: str, s2: str) -> bool. 'ba' appears inside 'eidbaooo' and has the same letters as 'ab', so the first sample is true. The starter file already has the function name and the harness print lines. Keep them, and fill only the function body.
Requirements
- Use a window with the same length as s1.
- Compare character counts, including duplicate characters.
- Return False when no matching window exists.
Constraints
- Matching is case-sensitive and the result is a boolean.
- The matching window must be contiguous in s2.
Examples
Input: check_inclusion('ab', 'eidbaooo') Output: True ba is a permutation window.
Topics: lakebench, python, permutation, window.
More Python interview questions · All interview problems · Learn data engineering
Permutation in String
Interview-style drill: Whether s2 contains any permutation of s1 as a substring.
Implement `check_inclusion(s1: str, s2: str) -> bool`. Example: `"ab", "eidbaooo"` → `True`. Keep the harness.