-
Notifications
You must be signed in to change notification settings - Fork 43
/
CompareStringsByFrequencyOfTheSmallestCharacter.java
309 lines (249 loc) · 9.55 KB
/
CompareStringsByFrequencyOfTheSmallestCharacter.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
package LeetCodeJava.HashTable;
// https://leetcode.com/problems/compare-strings-by-frequency-of-the-smallest-character/solutions/
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
public class CompareStringsByFrequencyOfTheSmallestCharacter {
// V0
// IDEA : HASH MAP + SORT + BINARY SEARCH (fixed by gpt)
public int[] numSmallerByFrequency(String[] queries, String[] words) {
int[] wordsFrequency = new int[words.length];
Map<String, Integer> frequencyMap = new HashMap<>();
// Calculate the frequency of the smallest character for each word and store in map
for (int i = 0; i < words.length; i++) {
wordsFrequency[i] = getFrequencyOfSmallestCharacter_(words[i], frequencyMap);
}
//System.out.println(">>> (before sort) wordsFrequency = " + Arrays.toString(wordsFrequency));
// sort, for binary search
Arrays.sort(wordsFrequency);
//System.out.println(">>> (after sort) wordsFrequency = " + Arrays.toString(wordsFrequency));
int[] result = new int[queries.length];
for (int i = 0; i < queries.length; i++) {
int queryFrequency = getFrequencyOfSmallestCharacter_(queries[i], frequencyMap);
result[i] = countGreater_(wordsFrequency, queryFrequency);
}
System.out.println(">>> result = " + Arrays.toString(result));
return result;
}
public int getFrequencyOfSmallestCharacter_(String word, Map<String, Integer> map){
int cnt = 1;
String prev = null;
for (String w : word.split("")){
//System.out.println("w = " + w + ", prev = " + prev + ", cnt = " + cnt);
if (prev == null){
prev = w;
cnt = 1;
}
else if (prev.compareTo(w) > 0){
prev = w;
cnt = 1;
}
else if (prev.equals(w)){
cnt += 1;
}
}
return cnt;
}
public int countGreater_(int[] wordFreq, int freq) {
// binary search (find bigger num idx)
int l = 0;
int r = wordFreq.length - 1;
while (l < r) {
int mid = (l + r) / 2;
if (wordFreq[mid] <= freq) {
l = mid + 1;
} else {
r = mid;
}
}
// After exiting the loop, `l` should be pointing to the first element greater than `freq`.
// If all elements are less than or equal to `freq`, `l` will be at the end of the array.
if (wordFreq[l] <= freq) {
return 0;
}
return wordFreq.length - l;
}
// V1
// IDEA : HASHMAP (gpt)
public int[] numSmallerByFrequency_1(String[] queries, String[] words) {
int[] wordsFrequency = new int[words.length];
Map<String, Integer> frequencyMap = new HashMap<>();
// Calculate the frequency of the smallest character for each word and store in map
for (int i = 0; i < words.length; i++) {
wordsFrequency[i] = getFrequencyOfSmallestCharacter(words[i], frequencyMap);
}
// Sort the frequencies for words to enable binary search
Arrays.sort(wordsFrequency);
int[] result = new int[queries.length];
// For each query, calculate the frequency of the smallest character
// and count the number of words with greater frequency
for (int i = 0; i < queries.length; i++) {
int queryFrequency = getFrequencyOfSmallestCharacter(queries[i], frequencyMap);
result[i] = countGreater(wordsFrequency, queryFrequency);
}
return result;
}
// Helper method to calculate the frequency of the smallest character in a string
private int getFrequencyOfSmallestCharacter(String s, Map<String, Integer> frequencyMap) {
if (frequencyMap.containsKey(s)) {
return frequencyMap.get(s);
}
char smallestChar = 'z';
int count = 0;
for (char c : s.toCharArray()) {
if (c < smallestChar) {
smallestChar = c;
count = 1;
} else if (c == smallestChar) {
count++;
}
}
frequencyMap.put(s, count);
return count;
}
// Helper method to count the number of elements in the array that are greater than the given value
private int countGreater(int[] arr, int value) {
int left = 0, right = arr.length;
while (left < right) {
int mid = left + (right - left) / 2;
if (arr[mid] <= value) {
left = mid + 1;
} else {
right = mid;
}
}
return arr.length - left;
}
// V1-1
// IDEA : ARRAY + BINARY SEARCH (gpt)
public int[] numSmallerByFrequency_1_1(String[] queries, String[] words) {
int[] wordsFrequency = new int[words.length];
// Calculate the frequency of the smallest character for each word
for (int i = 0; i < words.length; i++) {
wordsFrequency[i] = getFrequencyOfSmallestCharacter(words[i]);
}
// NOTE !!! Sort the frequencies for words to enable binary search
// binary search
Arrays.sort(wordsFrequency);
int[] result = new int[queries.length];
// For each query, calculate the frequency of the smallest character
// and count the number of words with greater frequency
for (int i = 0; i < queries.length; i++) {
/**
* NOTE !!!
*
* (func parameter)
*
* private int countGreater(int[] arr, int value)
*/
int queryFrequency = getFrequencyOfSmallestCharacter(queries[i]);
result[i] = countGreater_1(wordsFrequency, queryFrequency);
}
return result;
}
// Helper method to calculate the frequency of the smallest character in a string
private int getFrequencyOfSmallestCharacter(String s) {
char smallestChar = 'z';
int count = 0;
for (char c : s.toCharArray()) {
if (c < smallestChar) {
smallestChar = c;
count = 1;
} else if (c == smallestChar) {
count++;
}
}
return count;
}
// Helper method to count the number of elements in the array that are greater than the given value
// binary search
private int countGreater_1(int[] arr, int value) {
int left = 0, right = arr.length;
while (left < right) {
int mid = left + (right - left) / 2;
if (arr[mid] <= value) {
left = mid + 1;
} else {
right = mid;
}
}
return arr.length - left;
}
// V2
// https://leetcode.com/problems/compare-strings-by-frequency-of-the-smallest-character/submissions/1312707199/
public int[] numSmallerByFrequency_2(String[] queries, String[] words) {
int[] minCount = new int[11];
int[] aggregated = new int[11];
int[] result = new int[queries.length];
//build array with count of words by min count character. O(NK)
for (int i = 0; i < words.length; i++) {
minCount[minFunc(words[i])]++;
}
//build presum by count. O(1)
for (int i = 1; i < aggregated.length; i++) {
aggregated[i] += aggregated[i - 1] + minCount[i];
}
//find min count character in queries and set in result using presum O(MT)
for (int i = 0; i < queries.length; i++) {
int count = minFunc(queries[i]);
result[i] = aggregated[aggregated.length - 1] - aggregated[count];
}
return result;
}
private int minFunc(String s) {
int[] chars = new int[26];
int minCount = 0;
for (char c : s.toCharArray()) {
chars[c - 'a']++;
}
int i = 0;
while (minCount == 0) {
minCount = chars[i++];
}
return minCount;
}
// V3
// https://leetcode.com/problems/compare-strings-by-frequency-of-the-smallest-character/solutions/1200429/java-extremely-easy-solution-using-normal-strings-and-arraylist/
public int[] numSmallerByFrequency_3(String[] queries, String[] words) {
ArrayList<Integer> temp = new ArrayList<Integer>();
int count = 0;
for (int i = 0; i < queries.length; i++) {
for (int j = 0; j < words.length; j++) {
char temp1 = SmallestCharacter(queries[i]);
int freq1 = freqOfSmallestCharacter(queries[i], temp1);
char temp2 = SmallestCharacter(words[j]);
int freq2 = freqOfSmallestCharacter(words[j], temp2);
if (freq1 < freq2) {
count++;
}
}
temp.add(count);
count = 0;
}
int size = temp.size();
int[] ans = new int[size];
for (int i = 0; i < size; i++) {
ans[i] = temp.get(i);
}
return ans;
}
public int freqOfSmallestCharacter(String str, char a) {
int count = 0;
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == a) {
count++;
}
}
return count;
}
public char SmallestCharacter(String str) {
char temp = str.charAt(0);
for (int i = 0; i < str.length(); i++) {
if (temp > str.charAt(i)) {
temp = str.charAt(i);
}
}
return temp;
}
}