mediumSliding WindowPattern: Sliding Window / Hash Map

Substring Anagram Detection Solution

Problem Statement

Given a string `s` and a target string `t`, determine if any anagram of `t` exists as a contiguous substring within `s`. Return `true` if found, and `false` otherwise.

Examples

Example 1:
Input:{"keyPhrase":"abc","interceptedMessage":"abcdefgh"}
Output:true
Explanation: The key phrase 'abc' exists as a continuous segment in the intercepted message 'abcdefgh'
Example 2:
Input:{"keyPhrase":"xyz","interceptedMessage":"abcdefgh"}
Output:false
Explanation: The key phrase 'xyz' does not exist as a continuous segment in the intercepted message 'abcdefgh'

Constraints

  • 1 <= s1.length, s2.length <= 10^4
  • s1 and s2 consist of lowercase English letters.
Time: O(N) Space: O(1)
Fixed sliding window of size s1.length. Maintain character frequencies of s1 and current window in s2. If frequencies match, return true. Time O(N), Space O(1).

Run, Test & Submit Code

Ready to practice this challenge? Launch our interactive compilation environment with compiler validation.

Solve on Interactive Workspace

Tested Solutions

No solution code is currently loaded.
Complete this code in the workspace editor.