Menu
CoddyTech

Permutation in String

一个字符串的排列会使用相同的字母,顺序可以不同,每个字母出现的次数与原字符串相同:tar、rat 和 art 互为排列。给你两个由小写英文字母组成的字符串 s1 和 s2。如果 s1 的某种排列作为子字符串(连续的一段字符)出现在 s2 中,则返回 true;否则返回 false。

函数

checkInclusion(s1: string, s2: string) → boolean
s1string
要重新排列的字母
s2string
要在其中搜索的字符串
返回值boolean
当 s2 的某个子字符串是 s1 的一种重新排列时为 true

约束条件

  • 1 ≤ s1.length ≤ 2 × 104
  • 1 ≤ s2.length ≤ 5 × 104
  • s1 和 s2 中只包含小写英文字母(从 a 到 z)。
  • s1 的长度可能大于 s2。

示例

输入
s1 = "tar"s2 = "smartphone"
输出
true
解释
smartphone 中索引 2 到 4 处的子字符串 art 包含一个 a、一个 r 和一个 t,这些字母与 tar 相同。
challenge icon

进阶

你能否在 O(m + n) 时间内,返回 s2 中所有以 s1 的某种排列开头的索引?

重置代码
def checkInclusion(s1, s2):
    # 在这里编写代码

测试 #1

测试 #2

测试 #3

参数

s1 = "tar"
s2 = "smartphone"

预期输出

true