Skip to content

Commit 1297476

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

File tree

7 files changed

+241
-138
lines changed

7 files changed

+241
-138
lines changed

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

Lines changed: 82 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,15 @@
6666
```python
6767
class Solution:
6868
def numSmallerByFrequency(self, queries: List[str], words: List[str]) -> List[int]:
69-
def f(s):
69+
def f(s: str) -> int:
7070
cnt = Counter(s)
7171
for c in ascii_lowercase:
72-
if cnt[c]:
73-
return cnt[c]
72+
if x := cnt[c]:
73+
return x
7474

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]
75+
n = len(words)
76+
nums = sorted(f(w) for w in words)
77+
return [n - bisect_right(nums, f(q)) for q in queries]
7978
```
8079

8180
### **Java**
@@ -86,41 +85,37 @@ class Solution:
8685
class Solution {
8786
public int[] numSmallerByFrequency(String[] queries, String[] words) {
8887
int n = words.length;
89-
int[] arr = new int[n];
88+
int[] nums = new int[n];
9089
for (int i = 0; i < n; ++i) {
91-
arr[i] = f(words[i]);
90+
nums[i] = f(words[i]);
9291
}
93-
Arrays.sort(arr);
92+
Arrays.sort(nums);
9493
int m = queries.length;
9594
int[] ans = new int[m];
9695
for (int i = 0; i < m; ++i) {
9796
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;
97+
int l = 0, r = n;
98+
while (l < r) {
99+
int mid = (l + r) >> 1;
100+
if (nums[mid] > x) {
101+
r = mid;
102+
} else {
103+
l = mid + 1;
104+
}
111105
}
106+
ans[i] = n - l;
112107
}
113-
return left;
108+
return ans;
114109
}
115110

116111
private int f(String s) {
117112
int[] cnt = new int[26];
118113
for (int i = 0; i < s.length(); ++i) {
119114
++cnt[s.charAt(i) - 'a'];
120115
}
121-
for (int v : cnt) {
122-
if (v > 0) {
123-
return v;
116+
for (int x : cnt) {
117+
if (x > 0) {
118+
return x;
124119
}
125120
}
126121
return 0;
@@ -134,27 +129,28 @@ class Solution {
134129
class Solution {
135130
public:
136131
vector<int> numSmallerByFrequency(vector<string>& queries, vector<string>& words) {
137-
auto f = [](string& s) {
132+
auto f = [](string s) {
138133
int cnt[26] = {0};
139-
for (char& c : s) {
134+
for (char c : s) {
140135
cnt[c - 'a']++;
141136
}
142-
for (int i = 0; i < 26; ++i) {
143-
if (cnt[i]) {
144-
return cnt[i];
137+
for (int x : cnt) {
138+
if (x) {
139+
return x;
145140
}
146141
}
147142
return 0;
148143
};
149-
vector<int> arr;
150-
for (auto& s : words) {
151-
arr.emplace_back(f(s));
144+
int n = words.size();
145+
int nums[n];
146+
for (int i = 0; i < n; i++) {
147+
nums[i] = f(words[i]);
152148
}
153-
sort(arr.begin(), arr.end());
149+
sort(nums, nums + n);
154150
vector<int> ans;
155151
for (auto& q : queries) {
156152
int x = f(q);
157-
ans.emplace_back(arr.end() - upper_bound(arr.begin(), arr.end(), x));
153+
ans.push_back(n - (upper_bound(nums, nums + n, x) - nums));
158154
}
159155
return ans;
160156
}
@@ -170,27 +166,67 @@ func numSmallerByFrequency(queries []string, words []string) (ans []int) {
170166
for _, c := range s {
171167
cnt[c-'a']++
172168
}
173-
for _, v := range cnt {
174-
if v > 0 {
175-
return v
169+
for _, x := range cnt {
170+
if x > 0 {
171+
return x
176172
}
177173
}
178174
return 0
179175
}
180-
arr := []int{}
181-
for _, s := range words {
182-
arr = append(arr, f(s))
176+
n := len(words)
177+
nums := make([]int, n)
178+
for i, w := range words {
179+
nums[i] = f(w)
183180
}
184-
sort.Ints(arr)
185-
n := len(arr)
181+
sort.Ints(nums)
186182
for _, q := range queries {
187183
x := f(q)
188-
ans = append(ans, n-sort.Search(n, func(i int) bool { return arr[i] > x }))
184+
ans = append(ans, n-sort.SearchInts(nums, x+1))
189185
}
190186
return
191187
}
192188
```
193189

190+
### **TypeScript**
191+
192+
```ts
193+
function numSmallerByFrequency(queries: string[], words: string[]): number[] {
194+
const f = (s: string): number => {
195+
const cnt = new Array(26).fill(0);
196+
for (const c of s) {
197+
cnt[c.charCodeAt(0) - 'a'.charCodeAt(0)]++;
198+
}
199+
for (const x of cnt) {
200+
if (x) {
201+
return x;
202+
}
203+
}
204+
return 0;
205+
};
206+
const nums: number[] = [];
207+
for (const w of words) {
208+
nums.push(f(w));
209+
}
210+
nums.sort((a, b) => a - b);
211+
const ans: number[] = [];
212+
for (const q of queries) {
213+
const x = f(q);
214+
let l = 0,
215+
r = nums.length;
216+
while (l < r) {
217+
const mid = (l + r) >> 1;
218+
if (nums[mid] > x) {
219+
r = mid;
220+
} else {
221+
l = mid + 1;
222+
}
223+
}
224+
ans.push(nums.length - l);
225+
}
226+
return ans;
227+
}
228+
```
229+
194230
### **...**
195231

196232
```

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

Lines changed: 82 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,15 @@
4646
```python
4747
class Solution:
4848
def numSmallerByFrequency(self, queries: List[str], words: List[str]) -> List[int]:
49-
def f(s):
49+
def f(s: str) -> int:
5050
cnt = Counter(s)
5151
for c in ascii_lowercase:
52-
if cnt[c]:
53-
return cnt[c]
52+
if x := cnt[c]:
53+
return x
5454

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]
55+
n = len(words)
56+
nums = sorted(f(w) for w in words)
57+
return [n - bisect_right(nums, f(q)) for q in queries]
5958
```
6059

6160
### **Java**
@@ -64,41 +63,37 @@ class Solution:
6463
class Solution {
6564
public int[] numSmallerByFrequency(String[] queries, String[] words) {
6665
int n = words.length;
67-
int[] arr = new int[n];
66+
int[] nums = new int[n];
6867
for (int i = 0; i < n; ++i) {
69-
arr[i] = f(words[i]);
68+
nums[i] = f(words[i]);
7069
}
71-
Arrays.sort(arr);
70+
Arrays.sort(nums);
7271
int m = queries.length;
7372
int[] ans = new int[m];
7473
for (int i = 0; i < m; ++i) {
7574
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;
75+
int l = 0, r = n;
76+
while (l < r) {
77+
int mid = (l + r) >> 1;
78+
if (nums[mid] > x) {
79+
r = mid;
80+
} else {
81+
l = mid + 1;
82+
}
8983
}
84+
ans[i] = n - l;
9085
}
91-
return left;
86+
return ans;
9287
}
9388

9489
private int f(String s) {
9590
int[] cnt = new int[26];
9691
for (int i = 0; i < s.length(); ++i) {
9792
++cnt[s.charAt(i) - 'a'];
9893
}
99-
for (int v : cnt) {
100-
if (v > 0) {
101-
return v;
94+
for (int x : cnt) {
95+
if (x > 0) {
96+
return x;
10297
}
10398
}
10499
return 0;
@@ -112,27 +107,28 @@ class Solution {
112107
class Solution {
113108
public:
114109
vector<int> numSmallerByFrequency(vector<string>& queries, vector<string>& words) {
115-
auto f = [](string& s) {
110+
auto f = [](string s) {
116111
int cnt[26] = {0};
117-
for (char& c : s) {
112+
for (char c : s) {
118113
cnt[c - 'a']++;
119114
}
120-
for (int i = 0; i < 26; ++i) {
121-
if (cnt[i]) {
122-
return cnt[i];
115+
for (int x : cnt) {
116+
if (x) {
117+
return x;
123118
}
124119
}
125120
return 0;
126121
};
127-
vector<int> arr;
128-
for (auto& s : words) {
129-
arr.emplace_back(f(s));
122+
int n = words.size();
123+
int nums[n];
124+
for (int i = 0; i < n; i++) {
125+
nums[i] = f(words[i]);
130126
}
131-
sort(arr.begin(), arr.end());
127+
sort(nums, nums + n);
132128
vector<int> ans;
133129
for (auto& q : queries) {
134130
int x = f(q);
135-
ans.emplace_back(arr.end() - upper_bound(arr.begin(), arr.end(), x));
131+
ans.push_back(n - (upper_bound(nums, nums + n, x) - nums));
136132
}
137133
return ans;
138134
}
@@ -148,27 +144,67 @@ func numSmallerByFrequency(queries []string, words []string) (ans []int) {
148144
for _, c := range s {
149145
cnt[c-'a']++
150146
}
151-
for _, v := range cnt {
152-
if v > 0 {
153-
return v
147+
for _, x := range cnt {
148+
if x > 0 {
149+
return x
154150
}
155151
}
156152
return 0
157153
}
158-
arr := []int{}
159-
for _, s := range words {
160-
arr = append(arr, f(s))
154+
n := len(words)
155+
nums := make([]int, n)
156+
for i, w := range words {
157+
nums[i] = f(w)
161158
}
162-
sort.Ints(arr)
163-
n := len(arr)
159+
sort.Ints(nums)
164160
for _, q := range queries {
165161
x := f(q)
166-
ans = append(ans, n-sort.Search(n, func(i int) bool { return arr[i] > x }))
162+
ans = append(ans, n-sort.SearchInts(nums, x+1))
167163
}
168164
return
169165
}
170166
```
171167

168+
### **TypeScript**
169+
170+
```ts
171+
function numSmallerByFrequency(queries: string[], words: string[]): number[] {
172+
const f = (s: string): number => {
173+
const cnt = new Array(26).fill(0);
174+
for (const c of s) {
175+
cnt[c.charCodeAt(0) - 'a'.charCodeAt(0)]++;
176+
}
177+
for (const x of cnt) {
178+
if (x) {
179+
return x;
180+
}
181+
}
182+
return 0;
183+
};
184+
const nums: number[] = [];
185+
for (const w of words) {
186+
nums.push(f(w));
187+
}
188+
nums.sort((a, b) => a - b);
189+
const ans: number[] = [];
190+
for (const q of queries) {
191+
const x = f(q);
192+
let l = 0,
193+
r = nums.length;
194+
while (l < r) {
195+
const mid = (l + r) >> 1;
196+
if (nums[mid] > x) {
197+
r = mid;
198+
} else {
199+
l = mid + 1;
200+
}
201+
}
202+
ans.push(nums.length - l);
203+
}
204+
return ans;
205+
}
206+
```
207+
172208
### **...**
173209

174210
```

0 commit comments

Comments
 (0)