Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d8a2132

Browse files
feat: add solutions to lc problem: No.2531 (doocs#3325)
No.2531.Make Number of Distinct Characters Equal
1 parent 0d6cfe4 commit d8a2132

File tree

7 files changed

+351
-200
lines changed

7 files changed

+351
-200
lines changed

‎solution/2500-2599/2531.Make Number of Distinct Characters Equal/README.md‎

Lines changed: 119 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,15 @@ tags:
7070

7171
### 方法一:计数 + 枚举
7272

73-
我们先用两个长度为 26ドル$ 的数组分别统计字符串 $word1$ 和 $word2$ 中每个字母的出现次数,记为 $cnt1$ 和 $cnt2$
73+
我们先用两个长度为 26ドル$ 的数组 $\textit{cnt1}$ 和 $\textit{cnt2}$ 分别记录字符串 $\textit{word1}$ 和 $\textit{word2}$ 中每个字符的出现次数
7474

75-
然后我们枚举 $cnt1$ 中的每个字母,接着枚举 $cnt2$ 中的每个字母,如果 $cnt1[i]$ 和 $cnt2[j]$ 都不为 0ドル,ドル那么我们就可以交换 $word1$ 中的第 $i$ 个字母和 $word2$ 中的第 $j$ 个字母。如果交换后 $word1$ 和 $word2$ 中不同字母的数目相等,那么就返回 `true`,否则,我们撤销此次交换,继续枚举
75+
然后我们分别统计 $\textit{word1}$ 和 $\textit{word2}$ 中不同字符的个数,分别记为 $x$ 和 $y$
7676

77-
如果枚举完所有的字母对,仍然没有找到一种交换方式,那么就返回 `false`
77+
接下来我们枚举 $\textit{word1}$ 中的每个字符 $c1$ 和 $\textit{word2}$ 中的每个字符 $c2,ドル如果 $c1 = c2,ドル那么我们只需要判断 $x$ 和 $y$ 是否相等;否则,我们需要判断 $x - (\textit{cnt1}[c1] = 1) + (\textit{cnt1}[c2] = 0)$ 和 $y - (\textit{cnt2}[c2] = 1) + (\textit{cnt2}[c1] = 0)$ 是否相等。如果相等,那么我们就找到了一种方案,返回 $\text{true}$
7878

79-
时间复杂度 $O(n + C^3),ドル空间复杂度 $O(C),ドル其中 $n$ 是字符串的长度,而 $C$ 是字符集的大小。本题中 $C = 26$。
79+
如果我们枚举完所有的字符都没有找到合适的方案,那么我们就返回 $\text{false}$。
80+
81+
时间复杂度 $O(m + n + |\Sigma|^2),ドル其中 $m$ 和 $n$ 分别是字符串 $\textit{word1}$ 和 $\textit{word2}$ 的长度,而 $\Sigma$ 是字符集,本题中字符集为小写字母,所以 $|\Sigma| = 26$。
8082

8183
<!-- tabs:start -->
8284

@@ -85,21 +87,19 @@ tags:
8587
```python
8688
class Solution:
8789
def isItPossible(self, word1: str, word2: str) -> bool:
88-
cnt1 = [0] *26
89-
cnt2 = [0] *26
90-
for c in word1:
91-
cnt1[ord(c) -ord('a')] +=1
92-
for c in word2:
93-
cnt2[ord(c) -ord('a')] +=1
94-
for i, a inenumerate(cnt1):
95-
for j, b inenumerate(cnt2):
96-
if a and b:
97-
cnt1[i], cnt2[j] = cnt1[i] - 1, cnt2[j] -1
98-
cnt1[j], cnt2[i] = cnt1[j] +1, cnt2[i] +1
99-
if sum(v >0for v in cnt1) == sum(v >0for v in cnt2):
90+
cnt1 = Counter(word1)
91+
cnt2 = Counter(word2)
92+
x, y =len(cnt1), len(cnt2)
93+
for c1, v1 in cnt1.items():
94+
for c2, v2 in cnt2.items():
95+
if c1 == c2:
96+
if x == y:
97+
returnTrue
98+
else:
99+
a = x - (v1 ==1) + (cnt1[c2] ==0)
100+
b = y - (v2 ==1) + (cnt2[c1] ==0)
101+
if a == b:
100102
return True
101-
cnt1[i], cnt2[j] = cnt1[i] + 1, cnt2[j] + 1
102-
cnt1[j], cnt2[i] = cnt1[j] - 1, cnt2[i] - 1
103103
return False
104104
```
105105

@@ -110,35 +110,31 @@ class Solution {
110110
public boolean isItPossible(String word1, String word2) {
111111
int[] cnt1 = new int[26];
112112
int[] cnt2 = new int[26];
113+
int x = 0, y = 0;
113114
for (int i = 0; i < word1.length(); ++i) {
114-
++cnt1[word1.charAt(i) - 'a'];
115+
if (++cnt1[word1.charAt(i) - 'a'] == 1) {
116+
++x;
117+
}
115118
}
116119
for (int i = 0; i < word2.length(); ++i) {
117-
++cnt2[word2.charAt(i) - 'a'];
120+
if (++cnt2[word2.charAt(i) - 'a'] == 1) {
121+
++y;
122+
}
118123
}
119124
for (int i = 0; i < 26; ++i) {
120125
for (int j = 0; j < 26; ++j) {
121126
if (cnt1[i] > 0 && cnt2[j] > 0) {
122-
--cnt1[i];
123-
--cnt2[j];
124-
++cnt1[j];
125-
++cnt2[i];
126-
int d = 0;
127-
for (int k = 0; k < 26; ++k) {
128-
if (cnt1[k] > 0) {
129-
++d;
127+
if (i == j) {
128+
if (x == y) {
129+
return true;
130130
}
131-
if (cnt2[k] > 0) {
132-
--d;
131+
} else {
132+
int a = x - (cnt1[i] == 1 ? 1 : 0) + (cnt1[j] == 0 ? 1 : 0);
133+
int b = y - (cnt2[j] == 1 ? 1 : 0) + (cnt2[i] == 0 ? 1 : 0);
134+
if (a == b) {
135+
return true;
133136
}
134137
}
135-
if (d == 0) {
136-
return true;
137-
}
138-
++cnt1[i];
139-
++cnt2[j];
140-
--cnt1[j];
141-
--cnt2[i];
142138
}
143139
}
144140
}
@@ -155,35 +151,31 @@ public:
155151
bool isItPossible(string word1, string word2) {
156152
int cnt1[26]{};
157153
int cnt2[26]{};
154+
int x = 0, y = 0;
158155
for (char& c : word1) {
159-
++cnt1[c - 'a'];
156+
if (++cnt1[c - 'a'] == 1) {
157+
++x;
158+
}
160159
}
161160
for (char& c : word2) {
162-
++cnt2[c - 'a'];
161+
if (++cnt2[c - 'a'] == 1) {
162+
++y;
163+
}
163164
}
164165
for (int i = 0; i < 26; ++i) {
165166
for (int j = 0; j < 26; ++j) {
166167
if (cnt1[i] > 0 && cnt2[j] > 0) {
167-
--cnt1[i];
168-
--cnt2[j];
169-
++cnt1[j];
170-
++cnt2[i];
171-
int d = 0;
172-
for (int k = 0; k < 26; ++k) {
173-
if (cnt1[k] > 0) {
174-
++d;
168+
if (i == j) {
169+
if (x == y) {
170+
return true;
175171
}
176-
if (cnt2[k] > 0) {
177-
--d;
172+
} else {
173+
int a = x - (cnt1[i] == 1 ? 1 : 0) + (cnt1[j] == 0 ? 1 : 0);
174+
int b = y - (cnt2[j] == 1 ? 1 : 0) + (cnt2[i] == 0 ? 1 : 0);
175+
if (a == b) {
176+
return true;
178177
}
179178
}
180-
if (d == 0) {
181-
return true;
182-
}
183-
++cnt1[i];
184-
++cnt2[j];
185-
--cnt1[j];
186-
--cnt2[i];
187179
}
188180
}
189181
}
@@ -198,38 +190,96 @@ public:
198190
func isItPossible(word1 string, word2 string) bool {
199191
cnt1 := [26]int{}
200192
cnt2 := [26]int{}
193+
x, y := 0, 0
201194
for _, c := range word1 {
202195
cnt1[c-'a']++
196+
if cnt1[c-'a'] == 1 {
197+
x++
198+
}
203199
}
204200
for _, c := range word2 {
205201
cnt2[c-'a']++
202+
if cnt2[c-'a'] == 1 {
203+
y++
204+
}
206205
}
207206
for i := range cnt1 {
208207
for j := range cnt2 {
209208
if cnt1[i] > 0 && cnt2[j] > 0 {
210-
cnt1[i], cnt2[j] = cnt1[i]-1, cnt2[j]-1
211-
cnt1[j], cnt2[i] = cnt1[j]+1, cnt2[i]+1
212-
d := 0
213-
for k, a := range cnt1 {
214-
if a > 0 {
215-
d++
209+
if i == j {
210+
if x == y {
211+
return true
216212
}
217-
if cnt2[k] > 0 {
218-
d--
213+
} else {
214+
a := x
215+
if cnt1[i] == 1 {
216+
a--
217+
}
218+
if cnt1[j] == 0 {
219+
a++
220+
}
221+
222+
b := y
223+
if cnt2[j] == 1 {
224+
b--
225+
}
226+
if cnt2[i] == 0 {
227+
b++
228+
}
229+
230+
if a == b {
231+
return true
219232
}
220233
}
221-
if d == 0 {
222-
return true
223-
}
224-
cnt1[i], cnt2[j] = cnt1[i]+1, cnt2[j]+1
225-
cnt1[j], cnt2[i] = cnt1[j]-1, cnt2[i]-1
226234
}
227235
}
228236
}
229237
return false
230238
}
231239
```
232240

241+
#### TypeScript
242+
243+
```ts
244+
function isItPossible(word1: string, word2: string): boolean {
245+
const cnt1: number[] = Array(26).fill(0);
246+
const cnt2: number[] = Array(26).fill(0);
247+
let [x, y] = [0, 0];
248+
249+
for (const c of word1) {
250+
if (++cnt1[c.charCodeAt(0) - 'a'.charCodeAt(0)] === 1) {
251+
++x;
252+
}
253+
}
254+
255+
for (const c of word2) {
256+
if (++cnt2[c.charCodeAt(0) - 'a'.charCodeAt(0)] === 1) {
257+
++y;
258+
}
259+
}
260+
261+
for (let i = 0; i < 26; ++i) {
262+
for (let j = 0; j < 26; ++j) {
263+
if (cnt1[i] > 0 && cnt2[j] > 0) {
264+
if (i === j) {
265+
if (x === y) {
266+
return true;
267+
}
268+
} else {
269+
const a = x - (cnt1[i] === 1 ? 1 : 0) + (cnt1[j] === 0 ? 1 : 0);
270+
const b = y - (cnt2[j] === 1 ? 1 : 0) + (cnt2[i] === 0 ? 1 : 0);
271+
if (a === b) {
272+
return true;
273+
}
274+
}
275+
}
276+
}
277+
}
278+
279+
return false;
280+
}
281+
```
282+
233283
<!-- tabs:end -->
234284

235285
<!-- solution:end -->

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /