Re: [闲聊] 每日leetcode

楼主: JIWP (JIWP)   2024-10-06 10:23:56
567. Permutation in String
给两个字串s1、s2
会传true如果s2包含s1的permutation
思路:
纪录s1出现的每个字母次数
接着用two pointer去遍历s2
扣掉相对应的字母次数
当字母次数出现负数
就移动前指标,直到该字母次数变成正数
去判断前后指标相差的长度是不是等于s1的长度
是就回传true
golang code :
func checkInclusion(s1 string, s2 string) bool {
rec, n := make([]int, 26), len(s1)
idx1, idx2 := 0, 0
for i := 0; i < n; i++ {
rec[int(s1[i]-'a')]++
}
for idx2 < len(s2) {
tmp := int(s2[idx2] - 'a')
rec[tmp]

Links booklink

Contact Us: admin [ a t ] ucptt.com