Skip to content

Commit 4b810c0

Browse files
committed
feat: add solutions to lc problem: No.2287
No.2287.Rearrange Characters to Make Target String
1 parent 3901737 commit 4b810c0

File tree

4 files changed

+134
-0
lines changed

4 files changed

+134
-0
lines changed

solution/2200-2299/2287.Rearrange Characters to Make Target String/README.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,54 @@ function rearrangeCharacters(s: string, target: string): number {
178178
}
179179
```
180180

181+
### **Rust**
182+
183+
```rust
184+
impl Solution {
185+
pub fn rearrange_characters(s: String, target: String) -> i32 {
186+
let mut count1 = [0; 26];
187+
let mut count2 = [0; 26];
188+
for c in s.as_bytes() {
189+
count1[(c - b'a') as usize] += 1;
190+
}
191+
for c in target.as_bytes() {
192+
count2[(c - b'a') as usize] += 1;
193+
}
194+
let mut ans = i32::MAX;
195+
for i in 0..26 {
196+
if count2[i] != 0 {
197+
ans = ans.min(count1[i] / count2[i]);
198+
}
199+
}
200+
ans
201+
}
202+
}
203+
```
204+
205+
### **C**
206+
207+
```c
208+
#define min(a, b) (((a) < (b)) ? (a) : (b))
209+
210+
int rearrangeCharacters(char *s, char *target) {
211+
int count1[26] = {0};
212+
int count2[26] = {0};
213+
for (int i = 0; s[i]; i++) {
214+
count1[s[i] - 'a']++;
215+
}
216+
for (int i = 0; target[i]; i++) {
217+
count2[target[i] - 'a']++;
218+
}
219+
int ans = INT_MAX;
220+
for (int i = 0; i < 26; i++) {
221+
if (count2[i]) {
222+
ans = min(ans, count1[i] / count2[i]);
223+
}
224+
}
225+
return ans;
226+
}
227+
```
228+
181229
### **...**
182230
183231
```

solution/2200-2299/2287.Rearrange Characters to Make Target String/README_EN.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,54 @@ function rearrangeCharacters(s: string, target: string): number {
165165
}
166166
```
167167

168+
### **Rust**
169+
170+
```rust
171+
impl Solution {
172+
pub fn rearrange_characters(s: String, target: String) -> i32 {
173+
let mut count1 = [0; 26];
174+
let mut count2 = [0; 26];
175+
for c in s.as_bytes() {
176+
count1[(c - b'a') as usize] += 1;
177+
}
178+
for c in target.as_bytes() {
179+
count2[(c - b'a') as usize] += 1;
180+
}
181+
let mut ans = i32::MAX;
182+
for i in 0..26 {
183+
if count2[i] != 0 {
184+
ans = ans.min(count1[i] / count2[i]);
185+
}
186+
}
187+
ans
188+
}
189+
}
190+
```
191+
192+
### **C**
193+
194+
```c
195+
#define min(a, b) (((a) < (b)) ? (a) : (b))
196+
197+
int rearrangeCharacters(char *s, char *target) {
198+
int count1[26] = {0};
199+
int count2[26] = {0};
200+
for (int i = 0; s[i]; i++) {
201+
count1[s[i] - 'a']++;
202+
}
203+
for (int i = 0; target[i]; i++) {
204+
count2[target[i] - 'a']++;
205+
}
206+
int ans = INT_MAX;
207+
for (int i = 0; i < 26; i++) {
208+
if (count2[i]) {
209+
ans = min(ans, count1[i] / count2[i]);
210+
}
211+
}
212+
return ans;
213+
}
214+
```
215+
168216
### **...**
169217
170218
```
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#define min(a, b) (((a) < (b)) ? (a) : (b))
2+
3+
int rearrangeCharacters(char *s, char *target) {
4+
int count1[26] = {0};
5+
int count2[26] = {0};
6+
for (int i = 0; s[i]; i++) {
7+
count1[s[i] - 'a']++;
8+
}
9+
for (int i = 0; target[i]; i++) {
10+
count2[target[i] - 'a']++;
11+
}
12+
int ans = INT_MAX;
13+
for (int i = 0; i < 26; i++) {
14+
if (count2[i]) {
15+
ans = min(ans, count1[i] / count2[i]);
16+
}
17+
}
18+
return ans;
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
impl Solution {
2+
pub fn rearrange_characters(s: String, target: String) -> i32 {
3+
let mut count1 = [0; 26];
4+
let mut count2 = [0; 26];
5+
for c in s.as_bytes() {
6+
count1[(c - b'a') as usize] += 1;
7+
}
8+
for c in target.as_bytes() {
9+
count2[(c - b'a') as usize] += 1;
10+
}
11+
let mut ans = i32::MAX;
12+
for i in 0..26 {
13+
if count2[i] != 0 {
14+
ans = ans.min(count1[i] / count2[i]);
15+
}
16+
}
17+
ans
18+
}
19+
}

0 commit comments

Comments
 (0)