Skip to content

Commit 69f2966

Browse files
committed
feat: add solutions to lc problem: No.2287
No.2287.Rearrange Characters to Make Target String
1 parent 499cddd commit 69f2966

File tree

7 files changed

+105
-120
lines changed

7 files changed

+105
-120
lines changed

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

Lines changed: 39 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@
5656

5757
<!-- 这里可写通用的实现逻辑 -->
5858

59+
**方法一:计数 + 枚举**
60+
61+
我们统计字符串 `s``target` 中每个字符出现的次数,记为 `cnt1``cnt2`。对于 `target` 中的每个字符,我们计算 `cnt1` 中该字符出现的次数除以 `cnt2` 中该字符出现的次数,取最小值即可。
62+
63+
时间复杂度 $O(n + m)$,空间复杂度 $O(C)$。其中 $n$ 和 $m$ 分别是字符串 `s``target` 的长度。$C$ 是字符集的大小,本题中 $C=26$。
64+
5965
<!-- tabs:start -->
6066

6167
### **Python3**
@@ -65,14 +71,9 @@
6571
```python
6672
class Solution:
6773
def rearrangeCharacters(self, s: str, target: str) -> int:
68-
cnt = Counter(s)
74+
cnt1 = Counter(s)
6975
cnt2 = Counter(target)
70-
ans = inf
71-
for c, v in cnt2.items():
72-
if cnt[c] < v:
73-
return 0
74-
ans = min(ans, cnt[c] // v)
75-
return ans
76+
return min(cnt1[c] // v for c, v in cnt2.items())
7677
```
7778

7879
### **Java**
@@ -84,18 +85,15 @@ class Solution {
8485
public int rearrangeCharacters(String s, String target) {
8586
int[] cnt1 = new int[26];
8687
int[] cnt2 = new int[26];
87-
for (char c : s.toCharArray()) {
88-
++cnt1[c - 'a'];
88+
for (int i = 0; i < s.length(); ++i) {
89+
++cnt1[s.charAt(i) - 'a'];
8990
}
90-
for (char c : target.toCharArray()) {
91-
++cnt2[c - 'a'];
91+
for (int i = 0; i < target.length(); ++i) {
92+
++cnt2[target.charAt(i) - 'a'];
9293
}
9394
int ans = 100;
9495
for (int i = 0; i < 26; ++i) {
9596
if (cnt2[i] > 0) {
96-
if (cnt1[i] < cnt2[i]) {
97-
return 0;
98-
}
9997
ans = Math.min(ans, cnt1[i] / cnt2[i]);
10098
}
10199
}
@@ -110,15 +108,19 @@ class Solution {
110108
class Solution {
111109
public:
112110
int rearrangeCharacters(string s, string target) {
113-
vector<int> cnt1(26);
114-
vector<int> cnt2(26);
115-
for (char& c : s) ++cnt1[c - 'a'];
116-
for (char& c : target) ++cnt2[c - 'a'];
111+
int cnt1[26]{};
112+
int cnt2[26]{};
113+
for (char& c : s) {
114+
++cnt1[c - 'a'];
115+
}
116+
for (char& c : target) {
117+
++cnt2[c - 'a'];
118+
}
117119
int ans = 100;
118120
for (int i = 0; i < 26; ++i) {
119-
if (cnt2[i] <= 0) continue;
120-
if (cnt1[i] < cnt2[i]) return 0;
121-
ans = min(ans, cnt1[i] / cnt2[i]);
121+
if (cnt2[i]) {
122+
ans = min(ans, cnt1[i] / cnt2[i]);
123+
}
122124
}
123125
return ans;
124126
}
@@ -129,8 +131,7 @@ public:
129131
130132
```go
131133
func rearrangeCharacters(s string, target string) int {
132-
cnt1 := make([]int, 26)
133-
cnt2 := make([]int, 26)
134+
var cnt1, cnt2 [26]int
134135
for _, c := range s {
135136
cnt1[c-'a']++
136137
}
@@ -139,13 +140,9 @@ func rearrangeCharacters(s string, target string) int {
139140
}
140141
ans := 100
141142
for i, v := range cnt2 {
142-
if v <= 0 {
143-
continue
144-
}
145-
if cnt1[i] < v {
146-
return 0
143+
if v > 0 {
144+
ans = min(ans, cnt1[i]/v)
147145
}
148-
ans = min(ans, cnt1[i]/v)
149146
}
150147
return ans
151148
}
@@ -162,20 +159,22 @@ func min(a, b int) int {
162159

163160
```ts
164161
function rearrangeCharacters(s: string, target: string): number {
165-
let cnt1 = new Array(128).fill(0),
166-
cnt2 = new Array(128).fill(0);
167-
for (let i of target) {
168-
cnt1[i.charCodeAt(0)]++;
162+
const idx = (s: string) => s.charCodeAt(0) - 97;
163+
const cnt1 = new Array(26).fill(0);
164+
const cnt2 = new Array(26).fill(0);
165+
for (const c of s) {
166+
++cnt1[idx(c)];
169167
}
170-
for (let i of s) {
171-
cnt2[i.charCodeAt(0)]++;
168+
for (const c of target) {
169+
++cnt2[idx(c)];
172170
}
173-
let ans = Infinity;
174-
for (let i = 0; i < 128; i++) {
175-
if (cnt1[i] === 0) continue;
176-
ans = Math.min(ans, Math.floor(cnt2[i] / cnt1[i]));
171+
let ans = 100;
172+
for (let i = 0; i < 26; ++i) {
173+
if (cnt2[i]) {
174+
ans = Math.min(ans, Math.floor(cnt1[i] / cnt2[i]));
175+
}
177176
}
178-
return ans === Infinity ? 0 : ans;
177+
return ans;
179178
}
180179
```
181180

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

Lines changed: 33 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,9 @@ We can make at most one copy of &quot;aaaaa&quot;, so we return 1.
6060
```python
6161
class Solution:
6262
def rearrangeCharacters(self, s: str, target: str) -> int:
63-
cnt = Counter(s)
63+
cnt1 = Counter(s)
6464
cnt2 = Counter(target)
65-
ans = inf
66-
for c, v in cnt2.items():
67-
if cnt[c] < v:
68-
return 0
69-
ans = min(ans, cnt[c] // v)
70-
return ans
65+
return min(cnt1[c] // v for c, v in cnt2.items())
7166
```
7267

7368
### **Java**
@@ -77,18 +72,15 @@ class Solution {
7772
public int rearrangeCharacters(String s, String target) {
7873
int[] cnt1 = new int[26];
7974
int[] cnt2 = new int[26];
80-
for (char c : s.toCharArray()) {
81-
++cnt1[c - 'a'];
75+
for (int i = 0; i < s.length(); ++i) {
76+
++cnt1[s.charAt(i) - 'a'];
8277
}
83-
for (char c : target.toCharArray()) {
84-
++cnt2[c - 'a'];
78+
for (int i = 0; i < target.length(); ++i) {
79+
++cnt2[target.charAt(i) - 'a'];
8580
}
8681
int ans = 100;
8782
for (int i = 0; i < 26; ++i) {
8883
if (cnt2[i] > 0) {
89-
if (cnt1[i] < cnt2[i]) {
90-
return 0;
91-
}
9284
ans = Math.min(ans, cnt1[i] / cnt2[i]);
9385
}
9486
}
@@ -103,15 +95,19 @@ class Solution {
10395
class Solution {
10496
public:
10597
int rearrangeCharacters(string s, string target) {
106-
vector<int> cnt1(26);
107-
vector<int> cnt2(26);
108-
for (char& c : s) ++cnt1[c - 'a'];
109-
for (char& c : target) ++cnt2[c - 'a'];
98+
int cnt1[26]{};
99+
int cnt2[26]{};
100+
for (char& c : s) {
101+
++cnt1[c - 'a'];
102+
}
103+
for (char& c : target) {
104+
++cnt2[c - 'a'];
105+
}
110106
int ans = 100;
111107
for (int i = 0; i < 26; ++i) {
112-
if (cnt2[i] <= 0) continue;
113-
if (cnt1[i] < cnt2[i]) return 0;
114-
ans = min(ans, cnt1[i] / cnt2[i]);
108+
if (cnt2[i]) {
109+
ans = min(ans, cnt1[i] / cnt2[i]);
110+
}
115111
}
116112
return ans;
117113
}
@@ -122,8 +118,7 @@ public:
122118
123119
```go
124120
func rearrangeCharacters(s string, target string) int {
125-
cnt1 := make([]int, 26)
126-
cnt2 := make([]int, 26)
121+
var cnt1, cnt2 [26]int
127122
for _, c := range s {
128123
cnt1[c-'a']++
129124
}
@@ -132,13 +127,9 @@ func rearrangeCharacters(s string, target string) int {
132127
}
133128
ans := 100
134129
for i, v := range cnt2 {
135-
if v <= 0 {
136-
continue
130+
if v > 0 {
131+
ans = min(ans, cnt1[i]/v)
137132
}
138-
if cnt1[i] < v {
139-
return 0
140-
}
141-
ans = min(ans, cnt1[i]/v)
142133
}
143134
return ans
144135
}
@@ -155,20 +146,22 @@ func min(a, b int) int {
155146

156147
```ts
157148
function rearrangeCharacters(s: string, target: string): number {
158-
let cnt1 = new Array(128).fill(0),
159-
cnt2 = new Array(128).fill(0);
160-
for (let i of target) {
161-
cnt1[i.charCodeAt(0)]++;
149+
const idx = (s: string) => s.charCodeAt(0) - 97;
150+
const cnt1 = new Array(26).fill(0);
151+
const cnt2 = new Array(26).fill(0);
152+
for (const c of s) {
153+
++cnt1[idx(c)];
162154
}
163-
for (let i of s) {
164-
cnt2[i.charCodeAt(0)]++;
155+
for (const c of target) {
156+
++cnt2[idx(c)];
165157
}
166-
let ans = Infinity;
167-
for (let i = 0; i < 128; i++) {
168-
if (cnt1[i] === 0) continue;
169-
ans = Math.min(ans, Math.floor(cnt2[i] / cnt1[i]));
158+
let ans = 100;
159+
for (let i = 0; i < 26; ++i) {
160+
if (cnt2[i]) {
161+
ans = Math.min(ans, Math.floor(cnt1[i] / cnt2[i]));
162+
}
170163
}
171-
return ans === Infinity ? 0 : ans;
164+
return ans;
172165
}
173166
```
174167

solution/2200-2299/2287.Rearrange Characters to Make Target String/Solution.cpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
class Solution {
22
public:
33
int rearrangeCharacters(string s, string target) {
4-
vector<int> cnt1(26);
5-
vector<int> cnt2(26);
6-
for (char& c : s) ++cnt1[c - 'a'];
7-
for (char& c : target) ++cnt2[c - 'a'];
4+
int cnt1[26]{};
5+
int cnt2[26]{};
6+
for (char& c : s) {
7+
++cnt1[c - 'a'];
8+
}
9+
for (char& c : target) {
10+
++cnt2[c - 'a'];
11+
}
812
int ans = 100;
913
for (int i = 0; i < 26; ++i) {
10-
if (cnt2[i] <= 0) continue;
11-
if (cnt1[i] < cnt2[i]) return 0;
12-
ans = min(ans, cnt1[i] / cnt2[i]);
14+
if (cnt2[i]) {
15+
ans = min(ans, cnt1[i] / cnt2[i]);
16+
}
1317
}
1418
return ans;
1519
}

solution/2200-2299/2287.Rearrange Characters to Make Target String/Solution.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
func rearrangeCharacters(s string, target string) int {
2-
cnt1 := make([]int, 26)
3-
cnt2 := make([]int, 26)
2+
var cnt1, cnt2 [26]int
43
for _, c := range s {
54
cnt1[c-'a']++
65
}
@@ -9,13 +8,9 @@ func rearrangeCharacters(s string, target string) int {
98
}
109
ans := 100
1110
for i, v := range cnt2 {
12-
if v <= 0 {
13-
continue
11+
if v > 0 {
12+
ans = min(ans, cnt1[i]/v)
1413
}
15-
if cnt1[i] < v {
16-
return 0
17-
}
18-
ans = min(ans, cnt1[i]/v)
1914
}
2015
return ans
2116
}

solution/2200-2299/2287.Rearrange Characters to Make Target String/Solution.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,15 @@ class Solution {
22
public int rearrangeCharacters(String s, String target) {
33
int[] cnt1 = new int[26];
44
int[] cnt2 = new int[26];
5-
for (char c : s.toCharArray()) {
6-
++cnt1[c - 'a'];
5+
for (int i = 0; i < s.length(); ++i) {
6+
++cnt1[s.charAt(i) - 'a'];
77
}
8-
for (char c : target.toCharArray()) {
9-
++cnt2[c - 'a'];
8+
for (int i = 0; i < target.length(); ++i) {
9+
++cnt2[target.charAt(i) - 'a'];
1010
}
1111
int ans = 100;
1212
for (int i = 0; i < 26; ++i) {
1313
if (cnt2[i] > 0) {
14-
if (cnt1[i] < cnt2[i]) {
15-
return 0;
16-
}
1714
ans = Math.min(ans, cnt1[i] / cnt2[i]);
1815
}
1916
}
Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
class Solution:
22
def rearrangeCharacters(self, s: str, target: str) -> int:
3-
cnt = Counter(s)
3+
cnt1 = Counter(s)
44
cnt2 = Counter(target)
5-
ans = inf
6-
for c, v in cnt2.items():
7-
if cnt[c] < v:
8-
return 0
9-
ans = min(ans, cnt[c] // v)
10-
return ans
5+
return min(cnt1[c] // v for c, v in cnt2.items())
Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
function rearrangeCharacters(s: string, target: string): number {
2-
let cnt1 = new Array(128).fill(0),
3-
cnt2 = new Array(128).fill(0);
4-
for (let i of target) {
5-
cnt1[i.charCodeAt(0)]++;
2+
const idx = (s: string) => s.charCodeAt(0) - 97;
3+
const cnt1 = new Array(26).fill(0);
4+
const cnt2 = new Array(26).fill(0);
5+
for (const c of s) {
6+
++cnt1[idx(c)];
67
}
7-
for (let i of s) {
8-
cnt2[i.charCodeAt(0)]++;
8+
for (const c of target) {
9+
++cnt2[idx(c)];
910
}
10-
let ans = Infinity;
11-
for (let i = 0; i < 128; i++) {
12-
if (cnt1[i] === 0) continue;
13-
ans = Math.min(ans, Math.floor(cnt2[i] / cnt1[i]));
11+
let ans = 100;
12+
for (let i = 0; i < 26; ++i) {
13+
if (cnt2[i]) {
14+
ans = Math.min(ans, Math.floor(cnt1[i] / cnt2[i]));
15+
}
1416
}
15-
return ans === Infinity ? 0 : ans;
17+
return ans;
1618
}

0 commit comments

Comments
 (0)