Menu
CoddyTech

Permutation in String

A permutation of a string uses the same letters in any order, each one as many times as the original does: tar, rat and art are permutations of each other. You get two strings s1 and s2 made of lowercase English letters. Return true if some permutation of s1 appears in s2 as a substring (a run of consecutive characters), and false otherwise.

Function

checkInclusion(s1: string, s2: string) → boolean
s1string
the letters to rearrange
s2string
the string to search in
Returnsboolean
true if a substring of s2 is a rearrangement of s1

Constraints

  • 1 ≤ s1.length ≤ 2 × 104
  • 1 ≤ s2.length ≤ 5 × 104
  • s1 and s2 contain only lowercase English letters (a to z).
  • s1 may be longer than s2.

Examples

Input
s1 = "tar"s2 = "smartphone"
Output
true
Explanation
The substring art at indices 2 to 4 of smartphone holds one a, one r and one t, the same letters as tar.

lock icon+17 hidden tests on Submit

challenge icon

Follow-up

Can you return every index of s2 where a permutation of s1 starts, still in O(m + n) time?

Reset code
def checkInclusion(s1, s2):
    # Write code here
Test cases

Case 1

Case 2

Case 3

Input

s1 = "tar"
s2 = "smartphone"

Expected

true