-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathFindAllAnagramsInAString.java
More file actions
132 lines (123 loc) · 4.26 KB
/
Copy pathFindAllAnagramsInAString.java
File metadata and controls
132 lines (123 loc) · 4.26 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/**
* <p>给定两个字符串 <code>s</code> 和 <code>p</code>,找到 <code>s</code><strong> </strong>中所有 <code>p</code><strong> </strong>的 <strong>异位词 </strong>的子串,返回这些子串的起始索引。不考虑答案输出的顺序。</p>
*
* <p><strong>异位词 </strong>指由相同字母重排列形成的字符串(包括相同的字符串)。</p>
*
* <p> </p>
*
* <p><strong>示例 1:</strong></p>
*
* <pre>
* <strong>输入: </strong>s = "cbaebabacd", p = "abc"
* <strong>输出: </strong>[0,6]
* <strong>解释:</strong>
* 起始索引等于 0 的子串是 "cba", 它是 "abc" 的异位词。
* 起始索引等于 6 的子串是 "bac", 它是 "abc" 的异位词。
* </pre>
*
* <p><strong> 示例 2:</strong></p>
*
* <pre>
* <strong>输入: </strong>s = "abab", p = "ab"
* <strong>输出: </strong>[0,1,2]
* <strong>解释:</strong>
* 起始索引等于 0 的子串是 "ab", 它是 "ab" 的异位词。
* 起始索引等于 1 的子串是 "ba", 它是 "ab" 的异位词。
* 起始索引等于 2 的子串是 "ab", 它是 "ab" 的异位词。
* </pre>
*
* <p> </p>
*
* <p><strong>提示:</strong></p>
*
* <ul>
* <li><code>1 <= s.length, p.length <= 3 * 10<sup>4</sup></code></li>
* <li><code>s</code> 和 <code>p</code> 仅包含小写字母</li>
* </ul>
* <div><div>Related Topics</div><div><li>哈希表</li><li>字符串</li><li>滑动窗口</li></div></div><br><div><li>👍 759</li><li>👎 0</li></div>
*/
package leetcode9;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class FindAllAnagramsInAString {
public static void main(String[] args) {
new FindAllAnagramsInAString().new Solution().findAnagrams("baa", "aa");
}
/**
* 基于数组的计数法
* 注意:数据准备阶段、数据验证阶段,计数方式证号相反。「增加、减少相反」
*/
class Solution {
public List<Integer> findAnagrams(String s, String p) {
List<Integer> res = new ArrayList<>();
if (p.length() > s.length()) {
return res;
}
int[] memo = new int[26];
for (char c : p.toCharArray()) {
memo[c - 'a']--;
}
for (int i = 0; i < p.length(); i++) {
memo[s.charAt(i) - 'a']++;
}
calc(res, memo, 0);
for (int i = 0, j = p.length(); j < s.length(); i++, j++) {
memo[s.charAt(i) - 'a']--;
memo[s.charAt(j) - 'a']++;
calc(res, memo, i + 1);
}
return res;
}
private void calc(List<Integer> res, int[] memo, int i) {
for (int val : memo) {
if (val != 0) {
return;
}
}
res.add(i);
}
}
/**
* 基于Map的计数法
*/
class Solution2 {
public List<Integer> findAnagrams(String s, String p) {
List<Integer> res = new ArrayList<>();
if (p.length() > s.length()) {
return res;
}
Map<Character, Integer> map = new HashMap<>();
for (char c : p.toCharArray()) {
add(map, c);
}
for (int i = 0; i < p.length(); i++) {
remove(map, s.charAt(i));
}
if (map.isEmpty()) {
res.add(0);
}
for (int i = 0, j = p.length(); j < s.length(); i++, j++) {
add(map, s.charAt(i));
remove(map, s.charAt(j));
if (map.isEmpty()) {
res.add(i + 1);
}
}
return res;
}
private void add(Map<Character, Integer> map, char c) {
Integer val = map.get(c);
val = val == null ? 1 : val + 1;
if (val == 0) map.remove(c);
else map.put(c, val);
}
private void remove(Map<Character, Integer> map, char c) {
Integer val = map.get(c);
val = val == null ? -1 : val - 1;
if (val == 0) map.remove(c);
else map.put(c, val);
}
}
}