-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindAllAnagramsInAString.java
More file actions
81 lines (69 loc) · 3.12 KB
/
Copy pathFindAllAnagramsInAString.java
File metadata and controls
81 lines (69 loc) · 3.12 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
package com.leetcode.year_2020.sliding_window;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
/**
* https://leetcode.com/problems/find-all-anagrams-in-a-string/
* <p>
* Good Discussion :
* https://leetcode.com/problems/find-all-anagrams-in-a-string/discuss/92007/Sliding-Window-algorithm-template-to-solve-all-the-Leetcode-substring-search-problem.
*
* @author neeraj on 17/05/20
* Copyright (c) 2019, data-structures.
* All rights reserved.
*/
public class FindAllAnagramsInAString {
public static void main(String[] args) {
System.out.println(findAnagrams("forxxorfxdofr", "for"));
System.out.println(findAnagrams("abab", "ab"));
System.out.println(findAnagrams("cbaebabacd", "abc"));
System.out.println(findAnagrams("baa", "aa"));
System.out.println(findAnagrams("aaaaaaaaaa", "aa"));
}
public static List<Integer> findAnagrams(String s, String p) {
List<Integer> result = new LinkedList<>();
if (p.length() > s.length()) {
return result; // Invalid condition we can't find anagram in s.
}
Map<Character, Integer> charFreqMap = new HashMap<>();
for (char c : p.toCharArray()) {
charFreqMap.put(c, charFreqMap.getOrDefault(c, 0) + 1);
}
// Counter which will tell us if we have seen all characters from P, once it reaches 0.
// and this will store map.size() and not p.size() since p can contain duplicate like "bb".
int counter = charFreqMap.size();
// Our Sliding Window
int start = 0, end = 0;
// Expand the Sliding window till the end.
while (end < s.length()) {
// Validate last character of the Sliding window.
char charAtEndOfSlidingWindow = s.charAt(end);
if (charFreqMap.containsKey(charAtEndOfSlidingWindow)) {
charFreqMap.put(charAtEndOfSlidingWindow, charFreqMap.get(charAtEndOfSlidingWindow) - 1);
// Check if we have exhausted all occurrence of character at end of sliding window from p.
if (charFreqMap.get(charAtEndOfSlidingWindow) == 0) counter--;
}
end++;
// Now we have a window in which all characters exist, so let's invalidate that window by removing
// char at start.
while (counter == 0) {
char charAtStartOfSlidingWindow = s.charAt(start);
if (charFreqMap.containsKey(charAtStartOfSlidingWindow)) {
charFreqMap.put(charAtStartOfSlidingWindow, charFreqMap.get(charAtStartOfSlidingWindow) + 1);
// We are shrinking the window, so let's remove characters from the window.
// i.e Invalidate the window.
if (charFreqMap.get(charAtStartOfSlidingWindow) > 0) {
counter++;
}
}
// We found our answer.
if (end - start == p.length()) {
result.add(start);
}
start++;
}
}
return result;
}
}