Data Structures & Algorithms Sliding Window
💡
Exercise 44

Permutation in String 25 XP Medium

LeetCode Ctrl+Enter Run Ctrl+S Save

Given two strings s1 and s2, return True if s2 contains a permutation of s1.

In other words: does any substring of s2 contain exactly the same characters as s1 (same frequency)?

s1 = 'ab', s2 = 'eidbaooo' # Output: True — 'ba' is a permutation of 'ab'

Approach: Use a fixed-size sliding window of size len(s1) over s2. Compare character frequency counts using hash maps.

📋 Instructions
Implement `check_inclusion(s1, s2)` that checks if any permutation of s1 exists as a substring in s2. Test with: - `check_inclusion('ab', 'eidbaooo')` → True - `check_inclusion('ab', 'eidboaoo')` → False Print both results.
Count s1 characters. Slide a window of size len(s1) over s2, maintaining a count of window characters. If counts match, return True.
🧪 Test Cases
Input
check_inclusion('ab', 'eidbaooo')
Expected
True
Boolean check
Input
check_inclusion('ab', 'eidboaoo')
Expected
False
Boolean check
main.py
Hi! I'm Rex 👋
Output
Ready. Press ▶ Run or Ctrl+Enter.