File tree 1 file changed +49
-0
lines changed
1 file changed +49
-0
lines changed Original file line number Diff line number Diff line change
1
+ // https://leetcode.com/problems/compare-strings-by-frequency-of-the-smallest-character/
2
+
3
+ class Solution {
4
+ public int[] numSmallerByFrequency(String[] queries, String[] words) {
5
+ int[] answer = new int[queries.length];
6
+ int x[] = new int[queries.length];
7
+ int y[] = new int[words.length];
8
+
9
+ for (int i = 0; i < queries.length; i ++) {
10
+ int min = Integer.MAX_VALUE;
11
+ char minchar = '#';
12
+ HashMap<Character, Integer> map = new HashMap<>();
13
+ for (char ch : queries[i].toCharArray()) {
14
+ map.put(ch, map.getOrDefault(ch, 0) + 1);
15
+ if (min > (int)ch) {
16
+ min = (int)ch;
17
+ minchar = ch;
18
+ }
19
+ }
20
+ x[i] = map.get(minchar);
21
+ }
22
+
23
+ for (int i = 0; i < words.length; i ++) {
24
+ int min = Integer.MAX_VALUE;
25
+ char minchar = '#';
26
+ HashMap<Character, Integer> map = new HashMap<>();
27
+ for (char ch : words[i].toCharArray()) {
28
+ map.put(ch, map.getOrDefault(ch, 0) + 1);
29
+ if (min > (int)ch) {
30
+ min = (int)ch;
31
+ minchar = ch;
32
+ }
33
+ }
34
+ y[i] = map.get(minchar);
35
+ }
36
+
37
+ for (int i = 0; i < x.length; i ++) {
38
+ int tmp = 0;
39
+ for (int j = 0; j < y.length; j ++) {
40
+ // System.out.println(x[i]+" "+y[j]);
41
+ if (x[i] < y[j])
42
+ tmp ++;
43
+ }
44
+ answer[i] = tmp;
45
+ }
46
+
47
+ return answer;
48
+ }
49
+ }
You can’t perform that action at this time.
0 commit comments