BackmediumSliding Window Adobe

Permutation Inclusion Solution

Problem Statement

Given a key phrase and a longer intercepted message, return true if any rearrangement of the key phrase exists as a continuous segment within the message.

Example 1
Input
keyPhrase = 'xyz', message = 'abcxyzdef'
Output
false

Explanation: Step 1: Initialize two pointers, one for the key phrase and one for the message. Step 2: Compare the characters at the current positions of the key phrase and the message. If they match, move both pointers forward. If they don't match, move the pointer for the key phrase forward. Step 3: If the key phrase pointer reaches the end of the key phrase, it means we have found a permutation of the key phrase in the message, so return true. Otherwise, return false.

Example 2
Input
keyPhrase = 'xyz', message = 'xyzabcxyzdef'
Output
true

Explanation: Step 1: Initialize two pointers, one for the key phrase and one for the message. Step 2: Compare the characters at the current positions of the key phrase and the message. If they match, move both pointers forward. If they don't match, move the pointer for the key phrase forward. Step 3: If the key phrase pointer reaches the end of the key phrase, it means we have found a permutation of the key phrase in the message, so return true. Otherwise, return false.

Constraints

  • 1 <= s1.length, s2.length <= 10^4
  • s1 and s2 consist of lowercase English letters.
Live Compiler
Loading...
Test Cases & Output
🔒 Sign up to run your code

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free