File tree 3 files changed +142
-7
lines changed
Leetcode/0567.Permutation-in-String
3 files changed +142
-7
lines changed Original file line number Diff line number Diff line change @@ -81,3 +81,38 @@ func CheckInclusionSlice(s1 string, s2 string) bool {
81
81
}
82
82
return false
83
83
}
84
+
85
+ func CheckInclusion2 (s1 string , s2 string ) bool {
86
+ left , right := 0 , 0
87
+ need , windows := make (map [byte ]int ), make (map [byte ]int )
88
+ match := 0
89
+ for _ , v := range s1 {
90
+ need [byte (v )]++
91
+ }
92
+
93
+ for right < len (s2 ) {
94
+ cRight := s2 [right ]
95
+ windows [cRight ]++
96
+ right ++
97
+ if windows [cRight ] == need [cRight ] {
98
+ match ++
99
+ }
100
+
101
+ if match == len (need ) {
102
+ return true
103
+ }
104
+
105
+ if right - left >= len (s1 ) {
106
+ cLeft := s2 [left ]
107
+
108
+ // 如果左指針字符出現次數達到目標,減少 match
109
+ if windows [cLeft ] == need [cLeft ] {
110
+ match --
111
+ }
112
+ // 移動左指針
113
+ windows [cLeft ]--
114
+ left ++
115
+ }
116
+ }
117
+ return false
118
+ }
Original file line number Diff line number Diff line change @@ -7,16 +7,21 @@ var tests = []struct {
7
7
arg2 string
8
8
want bool
9
9
}{
10
+ // {
11
+ // "ab",
12
+ // "eidbaooo",
13
+ // true,
14
+ // },
15
+ // {
16
+ // "ab",
17
+ // "eidboaoo",
18
+ // false,
19
+ // },
10
20
{
11
- "ab " ,
12
- "eidbaooo " ,
21
+ "abcdxabcde " ,
22
+ "abcdeabcdx " ,
13
23
true ,
14
24
},
15
- {
16
- "ab" ,
17
- "eidboaoo" ,
18
- false ,
19
- },
20
25
}
21
26
22
27
func TestCheckInclusion (t * testing.T ) {
@@ -35,6 +40,14 @@ func TestCheckInclusionSlice(t *testing.T) {
35
40
}
36
41
}
37
42
43
+ func TestCheckInclusion2 (t * testing.T ) {
44
+ for _ , tt := range tests {
45
+ if got := CheckInclusion2 (tt .arg1 , tt .arg2 ); got != tt .want {
46
+ t .Errorf ("got = %v, want = %v" , got , tt .want )
47
+ }
48
+ }
49
+ }
50
+
38
51
func BenchmarkCheckInclusion (b * testing.B ) {
39
52
b .ResetTimer ()
40
53
for i := 0 ; i < b .N ; i ++ {
Original file line number Diff line number Diff line change
1
+ // 時間複雜度:
2
+ // 空間複雜度:
3
+ /*
4
+ * @lc app=leetcode.cn id=567 lang=golang
5
+ *
6
+ * [567] 字符串的排列
7
+ *
8
+ * https://leetcode.cn/problems/permutation-in-string/description/
9
+ *
10
+ * algorithms
11
+ * Medium (44.95%)
12
+ * Likes: 998
13
+ * Dislikes: 0
14
+ * Total Accepted: 287.5K
15
+ * Total Submissions: 639.1K
16
+ * Testcase Example: '"ab"\n"eidbaooo"'
17
+ *
18
+ * 给你两个字符串 s1 和 s2 ,写一个函数来判断 s2 是否包含 s1 的排列。如果是,返回 true ;否则,返回 false 。
19
+ *
20
+ * 换句话说,s1 的排列之一是 s2 的 子串 。
21
+ *
22
+ *
23
+ *
24
+ * 示例 1:
25
+ *
26
+ *
27
+ * 输入:s1 = "ab" s2 = "eidbaooo"
28
+ * 输出:true
29
+ * 解释:s2 包含 s1 的排列之一 ("ba").
30
+ *
31
+ *
32
+ * 示例 2:
33
+ *
34
+ *
35
+ * 输入:s1= "ab" s2 = "eidboaoo"
36
+ * 输出:false
37
+ *
38
+ *
39
+ *
40
+ *
41
+ * 提示:
42
+ *
43
+ *
44
+ * 1 <= s1.length, s2.length <= 10^4
45
+ * s1 和 s2 仅包含小写字母
46
+ *
47
+ *
48
+ */
49
+
50
+ // @lc code=start
51
+ func checkInclusion (s1 string , s2 string ) bool {
52
+ left , right := 0 , 0
53
+ need , windows := make (map [byte ]int ), make (map [byte ]int )
54
+ match := 0
55
+ for _ , v := range s1 {
56
+ need [byte (v )]++
57
+ }
58
+
59
+ for right < len (s2 ) {
60
+ cRight := s2 [right ]
61
+ windows [cRight ]++
62
+ right ++
63
+ if windows [cRight ] == need [cRight ] {
64
+ match ++
65
+ }
66
+
67
+ if match == len (need ) {
68
+ return true
69
+ }
70
+
71
+ if right - left >= len (s1 ) {
72
+ cLeft := s2 [left ]
73
+
74
+ // 如果左指針字符出現次數達到目標,減少 match
75
+ if windows [cLeft ] == need [cLeft ] {
76
+ match --
77
+ }
78
+ // 移動左指針
79
+ windows [cLeft ]--
80
+ left ++
81
+ }
82
+ }
83
+ return false
84
+ }
85
+
86
+ // @lc code=end
87
+
You can’t perform that action at this time.
0 commit comments