1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// @Title: 字符串的排列 (Permutation in String)
// @Author: 15816537946@163.com
// @Date: 2022-03-23 00:26:40
// @Runtime: 0 ms
// @Memory: 2.4 MB
func checkInclusion(s1 string, s2 string) bool {
    n, m := len(s1), len(s2)
    if n > m {
        return false
    }

    cnt := [26]int{}
    for i := range s1 {
        cnt[s1[i]-'a']--
    }

    var lo int
    for hi := range s2 {
        x := s2[hi]-'a'
        cnt[x]++

        for cnt[x] > 0 {
            cnt[s2[lo]-'a']--
            lo++
        }

        if hi-lo+1 == n {
            return true
        }
    }
    return false
}