Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 86ad427

Browse files
edit 438
1 parent d38ae97 commit 86ad427

File tree

2 files changed

+50
-22
lines changed

2 files changed

+50
-22
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ Your ideas/fixes/algorithms are more than welcome!
183183
|441|[Arranging Coins](https://leetcode.com/problems/arrange-coins/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_441.java)| O(n)|O(1) | Easy|
184184
|440|[K-th Smallest in Lexicographical Order](https://leetcode.com/problems/k-th-smallest-in-lexicographical-order/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_440.java)| O(n^2)|O(1) | Hard|
185185
|439|[Ternary Expression Parser](https://leetcode.com/problems/ternary-expression-parser/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_439.java)| O(n)|O(n) | Medium| Stack
186-
|438|[Find All Anagrams in a String](https://leetcode.com/problems/find-all-anagrams-in-a-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_438.java)| O(n)|O(1) | Easy|
186+
|438|[Find All Anagrams in a String](https://leetcode.com/problems/find-all-anagrams-in-a-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_438.java)| O(n)|O(1) | Easy| Sliding Window
187187
|437|[Path Sum III](https://leetcode.com/problems/path-sum-iii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_437.java) | O(n^2) |O(n) | Easy| DFS, recursion
188188
|436|[Find Right Interval](https://leetcode.com/problems/find-right-interval/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_436.java) | O(nlogn) |O(n) | Medium| Binary Search
189189
|435|[Non-overlapping Intervals](https://leetcode.com/problems/non-overlapping-intervals/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_435.java) | O(nlogn) |O(1) | Medium| Greedy

src/main/java/com/fishercoder/solutions/_438.java

Lines changed: 49 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,59 +3,87 @@
33
import java.util.ArrayList;
44
import java.util.List;
55

6+
/**438. Find All Anagrams in a String
7+
* Given a string s and a non-empty string p, find all the start indices of p's anagrams in s.
8+
Strings consists of lowercase English letters only and the length of both strings s and p will not be larger than 20,100.
9+
10+
The order of output does not matter.
11+
12+
Example 1:
13+
Input:
14+
s: "cbaebabacd" p: "abc"
15+
Output:
16+
[0, 6]
17+
18+
Explanation:
19+
The substring with start index = 0 is "cba", which is an anagram of "abc".
20+
The substring with start index = 6 is "bac", which is an anagram of "abc".
21+
22+
Example 2:
23+
Input:
24+
s: "abab" p: "ab"
25+
Output:
26+
[0, 1, 2]
27+
28+
Explanation:
29+
The substring with start index = 0 is "ab", which is an anagram of "ab".
30+
The substring with start index = 1 is "ba", which is an anagram of "ab".
31+
The substring with start index = 2 is "ab", which is an anagram of "ab".*/
32+
633
public class _438 {
7-
/**O(m*n) solution, my original and most intuitive one, but kind of brute force.*/
34+
/**
35+
* O(m*n) solution, my original and most intuitive one, but kind of brute force.
36+
*/
837
public List<Integer> findAnagrams(String s, String p) {
938
List<Integer> result = new ArrayList();
10-
for (int i = 0; i <= s.length()-p.length(); i++){
11-
if (isAnagram(s.substring(i, i+p.length()), p)) result.add(i);
39+
for (int i = 0; i <= s.length() - p.length(); i++) {
40+
if (isAnagram(s.substring(i, i + p.length()), p)) result.add(i);
1241
}
1342
return result;
1443
}
15-
16-
private boolean isAnagram(String s, String p){
44+
45+
private boolean isAnagram(String s, String p) {
1746
int[] c = new int[26];
18-
for (int i = 0; i < s.length(); i++){
47+
for (int i = 0; i < s.length(); i++) {
1948
c[s.charAt(i) - 'a']++;
2049
c[p.charAt(i) - 'a']--;
2150
}
22-
23-
for (int i : c){
24-
if(i != 0) return false;
51+
52+
for (int i : c) {
53+
if (i != 0) return false;
2554
}
2655
return true;
2756
}
2857

29-
58+
3059
static class SlidingWindowSolution {
31-
/**O(n) solution inspired by this post: https://discuss.leetcode.com/topic/64434/shortest-concise-java-o-n-sliding-window-solution*/
3260
public List<Integer> findAnagrams(String s, String p) {
3361
List<Integer> result = new ArrayList();
3462
int[] hash = new int[26];
35-
for (char c : p.toCharArray()){
63+
for (char c : p.toCharArray()) {
3664
hash[c - 'a']++;
3765
}
3866
int start = 0, end = 0, count = p.length();
39-
while (end < s.length()){
40-
if(hash[s.charAt(end) - 'a'] > 0){
67+
while (end < s.length()) {
68+
if (hash[s.charAt(end) - 'a'] > 0) {
4169
count--;
4270
}
4371
hash[s.charAt(end) - 'a']--;
4472
end++;
45-
46-
if(count == 0) result.add(start);
47-
48-
if((end - start) == p.length()){
49-
if(hash[s.charAt(start) - 'a'] >= 0) count++;
73+
74+
if (count == 0) result.add(start);
75+
76+
if ((end - start) == p.length()) {
77+
if (hash[s.charAt(start) - 'a'] >= 0) count++;
5078
hash[s.charAt(start) - 'a']++;
5179
start++;
5280
}
5381
}
5482
return result;
5583
}
5684
}
57-
58-
public static void main(String...args){
85+
86+
public static void main(String... args) {
5987
SlidingWindowSolution test = new SlidingWindowSolution();
6088
String s = "cbaebabacd";
6189
String p = "abc";

0 commit comments

Comments
 (0)