-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathFindAllAnagramsInAString.java
144 lines (108 loc) · 3.79 KB
/
FindAllAnagramsInAString.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
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
133
134
135
136
137
138
139
140
141
142
143
144
class Solution {
//Correct Approach --> Sliding Window
public List<Integer> findAnagrams(String s, String p) {
List<Integer> result = new ArrayList<>();
if(s.length()==0 || s.isEmpty()){
return result;
}
int[] charCounts = new int[26];
for(char c : p.toCharArray()){
charCounts[c-'a']++;
}
int left = 0;
int right = 0;
int count = p.length();
while(right < s.length()){
if(charCounts[s.charAt(right++)-'a']-- >=1){
count--;
}
if(count == 0){
result.add(left);
}
if(right - left == p.length() && charCounts[s.charAt(left++)-'a']++>=0){
count++;
}
}
return result;
}
}
class Solution {
public List<Integer> findAnagrams(String s, String p) {
// Char Frequency Map
int[] charCountS = new int[26];
int[] charCountP = new int[26];
// For Storing Result
List<Integer> result = new ArrayList<>();
// Storing Frequency Of Characters Of P String
for(char c : p.toCharArray()){
charCountP[c - 'a']++;
}
// 2 Pointers Which Will Move Maintaining The Window Of Length P
int start = 0;
int end = 0;
// Traversing Through The Loop
while(end < s.length()){
// Adding Frequency To The Map
charCountS[s.charAt(end) - 'a']++;
// Window Length - 1 For Index
if(end >= p.length() - 1){
// Because The Loop Runs Only 26 Time, We Consider It Constant TIme And Not Add It To Time Complexity
if(Arrays.equals(charCountS, charCountP)){
// Adding Starting Index
result.add(start);
}
// Removing Starting Character And Incrementing Start
charCountS[s.charAt(start)-'a']--;
start++;
}
// Incrementing End
end++;
}
// Returning Result
return result;
}
}
// Sorting Approach --> Should not be done --> will end up with TLE For extremely large strings
/*
class Solution {
public List<Integer> findAnagrams(String s, String p) {
List<Integer> list = new ArrayList<>();
// HashSet<Integer> set = new HashSet<Integer>();
if(s.length() < p.length()){
return list;
}
if(s.length() == p.length()){
int[] charCount = new int[128];
for(char c : s.toCharArray()){
charCount[c]++;
}
for(char c : p.toCharArray()){
charCount[c]++;
}
for(int i : charCount){
if(i%2 !=0){
return list;
}
}
list.add(0);
return list;
}
char[] checker = p.toCharArray();
Arrays.sort(checker);
p = new String(checker);
for(int i=0; i<s.length(); i++){
if(s.length() >= i+p.length()){
String str = s.substring(i,i+p.length());
char[] check = str.toCharArray();
Arrays.sort(check);
str = new String(check);
if(str.indexOf(p) != -1){
list.add(i);
}
}
}
// List<Integer> list = new ArrayList<>(set);
return list;
}
}
*/