-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathSolution.java
56 lines (51 loc) · 2.04 KB
/
Solution.java
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package ds.pointer.targetofferII014;
import java.util.HashMap;
import java.util.Map;
/**
* 字符串中的变位词
* 剑指 Offer II 014 https://leetcode-cn.com/problems/MPnaiL/
*
* @author yangyi 2022年02月25日17:53:03
*/
public class Solution {
public boolean checkInclusion(String s1, String s2) {
if (s2.length() < s1.length()) {
return false;
}
Map<Character, Integer> charToNums = new HashMap<>();
for (int i = 0; i < s1.length(); i++) {
charToNums.put(s1.charAt(i), charToNums.getOrDefault(s1.charAt(i), 0) + 1);
charToNums.put(s2.charAt(i), charToNums.getOrDefault(s2.charAt(i), 0) - 1);
}
if (allZero(charToNums)) {
return true;
}
//第2个for循环中的下标i相当于第2个指针,指向子字符串的最后一个字符。第1个指针指向下标为i-s1.length()的位置。
// 两个指针之间的子字符串的长度一直是字符串s1的长度
for (int i = s1.length(); i < s2.length(); i++) {
if (charToNums.containsKey(s2.charAt(i))) {
charToNums.put(s2.charAt(i), charToNums.getOrDefault(s2.charAt(i), 0) - 1);
}
if (charToNums.containsKey(s2.charAt(i - s1.length()))) {
charToNums.put(s2.charAt(i - s1.length()), charToNums.getOrDefault(s2.charAt(i - s1.length()), 0) + 1);
}
if (allZero(charToNums)) {
return true;
}
}
return false;
}
private boolean allZero(Map<Character, Integer> charToNums) {
for (Map.Entry<Character, Integer> kv : charToNums.entrySet()) {
if (kv.getValue() != 0) {
return false;
}
}
return true;
}
public static void main(String[] args) {
System.out.println(new Solution().checkInclusion("ab", "eidbaooo"));
System.out.println(new Solution().checkInclusion("ab", "eidboaoo"));
System.out.println(new Solution().checkInclusion("ab", "a"));
}
}