Skip to content

Commit a16dd4b

Browse files
committed
feat: add solutions to lc problem: No.1170
No.1170.Compare Strings by Frequency of the Smallest Character
1 parent 3fc9c24 commit a16dd4b

File tree

6 files changed

+352
-2
lines changed

6 files changed

+352
-2
lines changed

solution/1100-1199/1170.Compare Strings by Frequency of the Smallest Character/README.md

Lines changed: 127 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,22 +47,148 @@
4747

4848
<!-- 这里可写通用的实现逻辑 -->
4949

50+
**方法一:排序 + 二分查找**
51+
52+
我们先按照题目描述,实现函数 $f(s)$,函数返回字符串 $s$ 中按字典序比较最小字母的出现频次。
53+
54+
接下来,我们将 $words$ 中的每个字符串 $s$ 都计算出 $f(s)$,并将其排序,存放在数组 $arr$ 中。
55+
56+
最后,我们遍历 $queries$ 中的每个字符串 $s$,计算 $f(s)$,然后在 $arr$ 中二分查找第一个大于 $f(s)$ 的位置 $i$,则 $arr$ 中下标 $i$ 及其后面的元素都满足 $f(s) < f(W)$,其中 $W$ 表示 $words$ 中的每个字符串,因此当前查询的答案就是 $n - i$。
57+
58+
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 为 $words$ 的长度。
59+
5060
<!-- tabs:start -->
5161

5262
### **Python3**
5363

5464
<!-- 这里可写当前语言的特殊实现逻辑 -->
5565

5666
```python
57-
67+
class Solution:
68+
def numSmallerByFrequency(self, queries: List[str], words: List[str]) -> List[int]:
69+
def f(s):
70+
cnt = Counter(s)
71+
for c in ascii_lowercase:
72+
if cnt[c]:
73+
return cnt[c]
74+
75+
arr = [f(s) for s in words]
76+
arr.sort()
77+
n = len(arr)
78+
return [n - bisect_right(arr, f(q)) for q in queries]
5879
```
5980

6081
### **Java**
6182

6283
<!-- 这里可写当前语言的特殊实现逻辑 -->
6384

6485
```java
86+
class Solution {
87+
public int[] numSmallerByFrequency(String[] queries, String[] words) {
88+
int n = words.length;
89+
int[] arr = new int[n];
90+
for (int i = 0; i < n; ++i) {
91+
arr[i] = f(words[i]);
92+
}
93+
Arrays.sort(arr);
94+
int m = queries.length;
95+
int[] ans = new int[m];
96+
for (int i = 0; i < m; ++i) {
97+
int x = f(queries[i]);
98+
ans[i] = n - search(arr, x);
99+
}
100+
return ans;
101+
}
102+
103+
private int search(int[] arr, int x) {
104+
int left = 0, right = arr.length;
105+
while (left < right) {
106+
int mid = (left + right) >> 1;
107+
if (arr[mid] > x) {
108+
right = mid;
109+
} else {
110+
left = mid + 1;
111+
}
112+
}
113+
return left;
114+
}
115+
116+
private int f(String s) {
117+
int[] cnt = new int[26];
118+
for (int i = 0; i < s.length(); ++i) {
119+
++cnt[s.charAt(i) - 'a'];
120+
}
121+
for (int v : cnt) {
122+
if (v > 0) {
123+
return v;
124+
}
125+
}
126+
return 0;
127+
}
128+
}
129+
```
130+
131+
### **C++**
132+
133+
```cpp
134+
class Solution {
135+
public:
136+
vector<int> numSmallerByFrequency(vector<string>& queries, vector<string>& words) {
137+
auto f = [](string& s) {
138+
int cnt[26] = {0};
139+
for (char& c : s) {
140+
cnt[c - 'a']++;
141+
}
142+
for (int i = 0; i < 26; ++i) {
143+
if (cnt[i]) {
144+
return cnt[i];
145+
}
146+
}
147+
return 0;
148+
};
149+
vector<int> arr;
150+
for (auto& s : words) {
151+
arr.emplace_back(f(s));
152+
}
153+
sort(arr.begin(), arr.end());
154+
vector<int> ans;
155+
for (auto& q : queries) {
156+
int x = f(q);
157+
ans.emplace_back(arr.end() - upper_bound(arr.begin(), arr.end(), x));
158+
}
159+
return ans;
160+
}
161+
};
162+
```
65163
164+
### **Go**
165+
166+
```go
167+
func numSmallerByFrequency(queries []string, words []string) (ans []int) {
168+
f := func(s string) int {
169+
cnt := [26]int{}
170+
for _, c := range s {
171+
cnt[c-'a']++
172+
}
173+
for _, v := range cnt {
174+
if v > 0 {
175+
return v
176+
}
177+
}
178+
return 0
179+
}
180+
arr := []int{}
181+
for _, s := range words {
182+
arr = append(arr, f(s))
183+
}
184+
sort.Ints(arr)
185+
n := len(arr)
186+
for _, q := range queries {
187+
x := f(q)
188+
ans = append(ans, n-sort.Search(n, func(i int) bool { return arr[i] > x }))
189+
}
190+
return
191+
}
66192
```
67193

68194
### **...**

solution/1100-1199/1170.Compare Strings by Frequency of the Smallest Character/README_EN.md

Lines changed: 117 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,129 @@
4444
### **Python3**
4545

4646
```python
47-
47+
class Solution:
48+
def numSmallerByFrequency(self, queries: List[str], words: List[str]) -> List[int]:
49+
def f(s):
50+
cnt = Counter(s)
51+
for c in ascii_lowercase:
52+
if cnt[c]:
53+
return cnt[c]
54+
55+
arr = [f(s) for s in words]
56+
arr.sort()
57+
n = len(arr)
58+
return [n - bisect_right(arr, f(q)) for q in queries]
4859
```
4960

5061
### **Java**
5162

5263
```java
64+
class Solution {
65+
public int[] numSmallerByFrequency(String[] queries, String[] words) {
66+
int n = words.length;
67+
int[] arr = new int[n];
68+
for (int i = 0; i < n; ++i) {
69+
arr[i] = f(words[i]);
70+
}
71+
Arrays.sort(arr);
72+
int m = queries.length;
73+
int[] ans = new int[m];
74+
for (int i = 0; i < m; ++i) {
75+
int x = f(queries[i]);
76+
ans[i] = n - search(arr, x);
77+
}
78+
return ans;
79+
}
80+
81+
private int search(int[] arr, int x) {
82+
int left = 0, right = arr.length;
83+
while (left < right) {
84+
int mid = (left + right) >> 1;
85+
if (arr[mid] > x) {
86+
right = mid;
87+
} else {
88+
left = mid + 1;
89+
}
90+
}
91+
return left;
92+
}
93+
94+
private int f(String s) {
95+
int[] cnt = new int[26];
96+
for (int i = 0; i < s.length(); ++i) {
97+
++cnt[s.charAt(i) - 'a'];
98+
}
99+
for (int v : cnt) {
100+
if (v > 0) {
101+
return v;
102+
}
103+
}
104+
return 0;
105+
}
106+
}
107+
```
108+
109+
### **C++**
110+
111+
```cpp
112+
class Solution {
113+
public:
114+
vector<int> numSmallerByFrequency(vector<string>& queries, vector<string>& words) {
115+
auto f = [](string& s) {
116+
int cnt[26] = {0};
117+
for (char& c : s) {
118+
cnt[c - 'a']++;
119+
}
120+
for (int i = 0; i < 26; ++i) {
121+
if (cnt[i]) {
122+
return cnt[i];
123+
}
124+
}
125+
return 0;
126+
};
127+
vector<int> arr;
128+
for (auto& s : words) {
129+
arr.emplace_back(f(s));
130+
}
131+
sort(arr.begin(), arr.end());
132+
vector<int> ans;
133+
for (auto& q : queries) {
134+
int x = f(q);
135+
ans.emplace_back(arr.end() - upper_bound(arr.begin(), arr.end(), x));
136+
}
137+
return ans;
138+
}
139+
};
140+
```
53141
142+
### **Go**
143+
144+
```go
145+
func numSmallerByFrequency(queries []string, words []string) (ans []int) {
146+
f := func(s string) int {
147+
cnt := [26]int{}
148+
for _, c := range s {
149+
cnt[c-'a']++
150+
}
151+
for _, v := range cnt {
152+
if v > 0 {
153+
return v
154+
}
155+
}
156+
return 0
157+
}
158+
arr := []int{}
159+
for _, s := range words {
160+
arr = append(arr, f(s))
161+
}
162+
sort.Ints(arr)
163+
n := len(arr)
164+
for _, q := range queries {
165+
x := f(q)
166+
ans = append(ans, n-sort.Search(n, func(i int) bool { return arr[i] > x }))
167+
}
168+
return
169+
}
54170
```
55171

56172
### **...**
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
public:
3+
vector<int> numSmallerByFrequency(vector<string>& queries, vector<string>& words) {
4+
auto f = [](string& s) {
5+
int cnt[26] = {0};
6+
for (char& c : s) {
7+
cnt[c - 'a']++;
8+
}
9+
for (int i = 0; i < 26; ++i) {
10+
if (cnt[i]) {
11+
return cnt[i];
12+
}
13+
}
14+
return 0;
15+
};
16+
vector<int> arr;
17+
for (auto& s : words) {
18+
arr.emplace_back(f(s));
19+
}
20+
sort(arr.begin(), arr.end());
21+
vector<int> ans;
22+
for (auto& q : queries) {
23+
int x = f(q);
24+
ans.emplace_back(arr.end() - upper_bound(arr.begin(), arr.end(), x));
25+
}
26+
return ans;
27+
}
28+
};
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
func numSmallerByFrequency(queries []string, words []string) (ans []int) {
2+
f := func(s string) int {
3+
cnt := [26]int{}
4+
for _, c := range s {
5+
cnt[c-'a']++
6+
}
7+
for _, v := range cnt {
8+
if v > 0 {
9+
return v
10+
}
11+
}
12+
return 0
13+
}
14+
arr := []int{}
15+
for _, s := range words {
16+
arr = append(arr, f(s))
17+
}
18+
sort.Ints(arr)
19+
n := len(arr)
20+
for _, q := range queries {
21+
x := f(q)
22+
ans = append(ans, n-sort.Search(n, func(i int) bool { return arr[i] > x }))
23+
}
24+
return
25+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
class Solution {
2+
public int[] numSmallerByFrequency(String[] queries, String[] words) {
3+
int n = words.length;
4+
int[] arr = new int[n];
5+
for (int i = 0; i < n; ++i) {
6+
arr[i] = f(words[i]);
7+
}
8+
Arrays.sort(arr);
9+
int m = queries.length;
10+
int[] ans = new int[m];
11+
for (int i = 0; i < m; ++i) {
12+
int x = f(queries[i]);
13+
ans[i] = n - search(arr, x);
14+
}
15+
return ans;
16+
}
17+
18+
private int search(int[] arr, int x) {
19+
int left = 0, right = arr.length;
20+
while (left < right) {
21+
int mid = (left + right) >> 1;
22+
if (arr[mid] > x) {
23+
right = mid;
24+
} else {
25+
left = mid + 1;
26+
}
27+
}
28+
return left;
29+
}
30+
31+
private int f(String s) {
32+
int[] cnt = new int[26];
33+
for (int i = 0; i < s.length(); ++i) {
34+
++cnt[s.charAt(i) - 'a'];
35+
}
36+
for (int v : cnt) {
37+
if (v > 0) {
38+
return v;
39+
}
40+
}
41+
return 0;
42+
}
43+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
def numSmallerByFrequency(self, queries: List[str], words: List[str]) -> List[int]:
3+
def f(s):
4+
cnt = Counter(s)
5+
for c in ascii_lowercase:
6+
if cnt[c]:
7+
return cnt[c]
8+
9+
arr = [f(s) for s in words]
10+
arr.sort()
11+
n = len(arr)
12+
return [n - bisect_right(arr, f(q)) for q in queries]

0 commit comments

Comments
 (0)