Ctrl K

Hidden Anagram

Medium
You are given strings pattern and text. Check whether text contains a run of consecutive characters made of exactly the same letters as pattern, in the same quantities, possibly in a different order. Return True if such a run exists and False otherwise.
Example 1
pattern="tea", text="sweater" → True

The piece eat (positions 2–4) is a rearrangement of tea.

Example 2
pattern="moon", text="lemonade" → False

lemonade has only one o, while moon needs two.

Example 3
pattern="abc", text="ab" → False
Constraints
  • 1 ≤ len(pattern), len(text) ≤ 10⁵
  • Both strings consist of lowercase English letters
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsLongest Repeat-Free Stretch MediumTwo-Letter Stretch MediumRecurring Gene Windows MediumWindow with Few Kinds of Letters MediumEvery Letter at Least k Times Medium
loading Python…
Input
pattern="tea", text="sweater"
Expected
True

Run checks the first 3 tests in your browser. Submit checks all 98 tests on the server, hidden ones included.