Skip to content

Commit 6a9abda

Browse files
authored
Create 1170_Compare_strings_by_frequency
1 parent 1105df4 commit 6a9abda

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
}

0 commit comments

Comments
 (0)