Permutation in String

Medium#567fixed size

You get two strings, s1 and s2. Find out whether s2 contains s1 with its letters in some order, as one piece of letters in a row. Our example: s1 = "loot", s2 = "tollotool" — yes: "loto" at index 3.

input
s1, s2: two strings of lowercase English letters.
returns
true if s2 has the letters of s1 in some order as one piece of letters in a row, otherwise false.
constraints
  • 1 ≤ s1.length, s2.length ≤ 10⁴
  • s1 and s2 have lowercase English letters only
our 13 tests keep to them
examples
Open a test in the tests panel: it shows the input and the expected answer.
/**
 * @param {string} s1
 * @param {string} s2
 * @return {boolean}
 */
function checkInclusion(s1, s2) {

}
Ctrl ↵
format
reset

saved in this browser

13 tests · limit 3 s

Stuck?

See the solution run step by step on our example, then why it works and its complexity.

Watch the walkthrough →

Next: more sliding window problems

Pattern “Sliding window” →

The problem and its statement belong to LeetCode. Here are our own retelling, example and solution. Patterns Hero is not affiliated with LeetCode.