File tree Expand file tree Collapse file tree 1 file changed +90
-0
lines changed
Expand file tree Collapse file tree 1 file changed +90
-0
lines changed Original file line number Diff line number Diff line change 1+ /*
2+ * @lc app=leetcode.cn id=567 lang=java
3+ *
4+ * [567] 字符串的排列
5+ *
6+ * https://leetcode-cn.com/problems/permutation-in-string/description/
7+ *
8+ * algorithms
9+ * Medium (35.79%)
10+ * Likes: 151
11+ * Dislikes: 0
12+ * Total Accepted: 33.3K
13+ * Total Submissions: 91.4K
14+ * Testcase Example: '"ab"\n"eidbaooo"'
15+ *
16+ * 给定两个字符串 s1 和 s2,写一个函数来判断 s2 是否包含 s1 的排列。
17+ *
18+ * 换句话说,第一个字符串的排列之一是第二个字符串的子串。
19+ *
20+ * 示例1:
21+ *
22+ *
23+ * 输入: s1 = "ab" s2 = "eidbaooo"
24+ * 输出: True
25+ * 解释: s2 包含 s1 的排列之一 ("ba").
26+ *
27+ *
28+ *
29+ *
30+ * 示例2:
31+ *
32+ *
33+ * 输入: s1= "ab" s2 = "eidboaoo"
34+ * 输出: False
35+ *
36+ *
37+ *
38+ *
39+ * 注意:
40+ *
41+ *
42+ * 输入的字符串只包含小写字母
43+ * 两个字符串的长度都在 [1, 10,000] 之间
44+ *
45+ *
46+ */
47+
48+ // @lc code=start
49+ class Solution {
50+ public boolean checkInclusion (String s1 , String s2 ) {
51+ int [] need = new int [128 ];
52+ int [] window = new int [128 ];
53+ for (char ch : s1 .toCharArray ()) {
54+ need [ch ]++;
55+ }
56+
57+ int left = 0 , right = 0 ;
58+ int count = 0 ;
59+
60+ while (right < s2 .length ()) {
61+ char ch = s2 .charAt (right );
62+ right ++;
63+ if (need [ch ] > 0 ) {
64+ window [ch ]++;
65+ if (window [ch ] <= need [ch ]) {
66+ count ++;
67+ }
68+ }
69+
70+ while (right - left >= s1 .length ()) {
71+ if (count == s1 .length ()) {
72+ return true ;
73+ }
74+
75+ ch = s2 .charAt (left );
76+ left ++;
77+ if (need [ch ] > 0 ) {
78+ if (window [ch ] <= need [ch ]) {
79+ count --;
80+ }
81+ window [ch ]--;
82+ }
83+ }
84+ }
85+
86+ return false ;
87+ }
88+ }
89+ // @lc code=end
90+
You can’t perform that action at this time.
0 commit comments