Permutation in String

IF
AlgoAxiomStaff Engineers
JSTS
Medium20 mins

Given two strings s1 and s2, return true if s2 contains a permutation of s1. In other words, return true if one of s1's permutations is a substring of s2.

Examples

Example 1:

Input: s1 = "ab", s2 = "eidbaooo"

Output: true

Explanation: s2 contains one permutation of s1 ("ba").

Example 2:

Input: s1 = "ab", s2 = "eidboaoo"

Output: false

Explanation: No permutation of s1 exists as a contiguous substring in s2.

Example 3:

Input: s1 = "adc", s2 = "dcda"

Output: true

Explanation: s2 contains the permutation "dca" starting at index 1.

Constraints

  • 1 <= s1.length, s2.length <= 10⁴
  • s1 and s2 consist of lowercase English letters only.
Source: Two Pointers pattern — AlgoAxiom
JavaScript
Test Case 1
root = [1, 2, 3]
Test Case 2
root = [1, 2, 3, 4, 5]
Idle