Skip to content

Commit 70a84b4

Browse files
authored
feat: add typescript solution to lc problem: No.1737 (#650)
No.1737.Change Minimum Characters to Satisfy One of Three Conditions
1 parent 457c013 commit 70a84b4

File tree

3 files changed

+88
-0
lines changed

3 files changed

+88
-0
lines changed

solution/1700-1799/1737.Change Minimum Characters to Satisfy One of Three Conditions/README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
## 解法
5151

5252
<!-- 这里可写通用的实现逻辑 -->
53+
前缀和
5354

5455
<!-- tabs:start -->
5556

@@ -69,6 +70,36 @@
6970

7071
```
7172

73+
### **TypeScript**
74+
75+
```ts
76+
function minCharacters(a: string, b: string): number {
77+
const m = a.length, n = b.length;
78+
let count1 = new Array(26).fill(0);
79+
let count2 = new Array(26).fill(0);
80+
const base = 'a'.charCodeAt(0);
81+
82+
for (let char of a) {
83+
count1[char.charCodeAt(0) - base]++;
84+
}
85+
for (let char of b) {
86+
count2[char.charCodeAt(0) - base]++;
87+
}
88+
89+
let pre1 = 0, pre2 = 0;
90+
let ans = m + n;
91+
for (let i = 0; i < 25; i++) {
92+
pre1 += count1[i];
93+
pre2 += count2[i];
94+
// case1, case2, case3
95+
ans = Math.min(ans, m - pre1 + pre2, pre1 + n - pre2, m + n - count1[i] - count2[i]);
96+
}
97+
ans = Math.min(ans, m + n - count1[25] - count2[25]);
98+
99+
return ans;
100+
};
101+
```
102+
72103
### **...**
73104

74105
```

solution/1700-1799/1737.Change Minimum Characters to Satisfy One of Three Conditions/README_EN.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ The best way was done in 2 operations (either condition 1 or condition 3).
4747

4848
## Solutions
4949

50+
Prefix Sum
51+
5052
<!-- tabs:start -->
5153

5254
### **Python3**
@@ -61,6 +63,36 @@ The best way was done in 2 operations (either condition 1 or condition 3).
6163

6264
```
6365

66+
### **TypeScript**
67+
68+
```ts
69+
function minCharacters(a: string, b: string): number {
70+
const m = a.length, n = b.length;
71+
let count1 = new Array(26).fill(0);
72+
let count2 = new Array(26).fill(0);
73+
const base = 'a'.charCodeAt(0);
74+
75+
for (let char of a) {
76+
count1[char.charCodeAt(0) - base]++;
77+
}
78+
for (let char of b) {
79+
count2[char.charCodeAt(0) - base]++;
80+
}
81+
82+
let pre1 = 0, pre2 = 0;
83+
let ans = m + n;
84+
for (let i = 0; i < 25; i++) {
85+
pre1 += count1[i];
86+
pre2 += count2[i];
87+
// case1, case2, case3
88+
ans = Math.min(ans, m - pre1 + pre2, pre1 + n - pre2, m + n - count1[i] - count2[i]);
89+
}
90+
ans = Math.min(ans, m + n - count1[25] - count2[25]);
91+
92+
return ans;
93+
};
94+
```
95+
6496
### **...**
6597

6698
```
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
function minCharacters(a: string, b: string): number {
2+
const m = a.length, n = b.length;
3+
let count1 = new Array(26).fill(0);
4+
let count2 = new Array(26).fill(0);
5+
const base = 'a'.charCodeAt(0);
6+
7+
for (let char of a) {
8+
count1[char.charCodeAt(0) - base]++;
9+
}
10+
for (let char of b) {
11+
count2[char.charCodeAt(0) - base]++;
12+
}
13+
14+
let pre1 = 0, pre2 = 0;
15+
let ans = m + n;
16+
for (let i = 0; i < 25; i++) {
17+
pre1 += count1[i];
18+
pre2 += count2[i];
19+
// case1, case2, case3
20+
ans = Math.min(ans, m - pre1 + pre2, pre1 + n - pre2, m + n - count1[i] - count2[i]);
21+
}
22+
ans = Math.min(ans, m + n - count1[25] - count2[25]);
23+
24+
return ans;
25+
};

0 commit comments

Comments
 (0)