Skip to content

Commit 6c5fe12

Browse files
committed
254th Commit
1 parent 0c820c1 commit 6c5fe12

File tree

3 files changed

+114
-26
lines changed

3 files changed

+114
-26
lines changed

README.md

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,19 +60,20 @@ Ace Coding Interview with 75 Qs
6060
<details>
6161
<summary>Problems</summary>
6262

63-
| Array / String | | |
64-
| ---------------------------------------------- | ---------------- | ------ |
65-
| 1768. Merge Strings Alternately | [Solution][1768] | Easy |
66-
| 1071. Greatest Common Divisor of Strings | [Solution][1071] | Easy |
67-
| 1431. Kids With the Greatest Number of Candies | [Solution][1431] | Easy |
68-
| 605. Can Place Flowers | [Solution][605] | Easy |
69-
| 345. Reverse Vowels of a String | [Solution][345] | Easy |
70-
| 151. Reverse Words in a String | [Solution][151] | Medium |
71-
| 238. Product of Array Except Self | [Solution][238] | Medium |
72-
| 334. Increasing Triplet Subsequence | [Solution][334] | Medium |
73-
| 443. String Compression | [Solution][443] | Medium |
63+
| Array / String | | | |
64+
| ---------------------------------------------- | ---------------- | ------ | ------------------------ |
65+
| 1768. Merge Strings Alternately | [Solution][1768] | Easy | [詳解][1768-explanation] |
66+
| 1071. Greatest Common Divisor of Strings | [Solution][1071] | Easy | 詳解 |
67+
| 1431. Kids With the Greatest Number of Candies | [Solution][1431] | Easy | 詳解 |
68+
| 605. Can Place Flowers | [Solution][605] | Easy | 詳解 |
69+
| 345. Reverse Vowels of a String | [Solution][345] | Easy | 詳解 |
70+
| 151. Reverse Words in a String | [Solution][151] | Medium | 詳解 |
71+
| 238. Product of Array Except Self | [Solution][238] | Medium | 詳解 |
72+
| 334. Increasing Triplet Subsequence | [Solution][334] | Medium | 詳解 |
73+
| 443. String Compression | [Solution][443] | Medium | 詳解 |
7474

7575
[1768]: ./src/page-17/1768.%20Merge%20Strings%20Alternately/mergeAlternately.ts
76+
[1768-explanation]: ./src/page-17/1768.%20Merge%20Strings%20Alternately/README.md
7677
[1071]: ./src/page-11/1071.%20Greatest%20Common%20Divisor%20of%20Strings/gcdOfStrings.ts
7778
[1431]: ./src/page-14/1431.%20Kids%20With%20the%20Greatest%20Number%20of%20Candies/kidsWithCandies.ts
7879
[605]: ./src/page-6/605.%20Can%20Place%20Flowers/canPlaceFlowers.ts
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# 1768. 交替合併字串 (Merge Strings Alternately)
2+
3+
給你兩個字串 `word1``word2`。請你從 `word1` 開始,透過交替加上字母來合併字串。如果一個字串比另一個字串長,就將多出來的字母追加到合併後字串的結尾。
4+
5+
返回*合併後的字串*
6+
7+
範例 1:
8+
9+
```ts
10+
輸入: word1 = "abc", word2 = "pqr"
11+
輸出: "apbqcr"
12+
說明: 字串合併情況如下所示:
13+
word1: a b c
14+
word2: p q r
15+
合併後: a p b q c r
16+
```
17+
18+
範例 2:
19+
20+
```ts
21+
輸入: word1 = "ab", word2 = "pqrs"
22+
輸出: "apbqrs"
23+
說明: 注意,word2word1 長,"rs" 需要追加到合併後字串的結尾。
24+
word1: a b
25+
word2: p q r s
26+
合併後: a p b q r s
27+
```
28+
29+
範例 3:
30+
31+
```ts
32+
輸入: word1 = "abcd", word2 = "pq"
33+
輸出: "apbqcd"
34+
說明: 注意,word1word2 長,"cd" 需要追加到合併後字串的結尾。
35+
word1: a b c d
36+
word2: p q
37+
合併後: a p b q c d
38+
```
39+
40+
## 解題
41+
42+
```ts
43+
export const mergeAlternately: MergeAlternately = (word1, word2) => {
44+
const chars1 = Array.from(word1);
45+
const chars2 = Array.from(word2);
46+
47+
// 使用 flatMap 方法,將兩個字串的字元交替合併
48+
let mergedChars = chars1.flatMap((char, index) => [char, chars2[index]]);
49+
50+
// 如果 chars2 比 chars1 長,說明還有剩下的字元需要附加到結果陣列中
51+
if (chars2.length > chars1.length) {
52+
mergedChars = [...mergedChars, ...chars2.slice(chars1.length)];
53+
}
54+
55+
return mergedChars.join('');
56+
};
57+
```
58+
59+
## 解題 2
60+
61+
使用雙指標 (Two Pointers)
62+
63+
```ts
64+
export const mergeAlternately2: MergeAlternately = (word1, word2) => {
65+
let result = ''; // 儲存合併後的結果
66+
67+
let pointer1 = 0; // 指向 word1 的當前位置
68+
let pointer2 = 0; // 指向 word2 的當前位置
69+
70+
// 當兩個指標都還在有效範圍時,交替加入字元
71+
while (pointer1 < word1.length || pointer2 < word2.length) {
72+
if (pointer1 < word1.length) {
73+
// 從 word1 中取一個字元並加入結果,然後遞增指標
74+
result += word1[pointer1];
75+
pointer1 += 1;
76+
}
77+
78+
if (pointer2 < word2.length) {
79+
// 從 word2 中取一個字元並加入結果,然後遞增指標
80+
result += word2[pointer2];
81+
pointer2 += 1;
82+
}
83+
}
84+
85+
return result;
86+
};
87+
```

src/page-17/1768. Merge Strings Alternately/mergeAlternately.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@ type MergeAlternately = (word1: string, word2: string) => string;
44
* Accepted
55
*/
66
export const mergeAlternately: MergeAlternately = (word1, word2) => {
7-
const array1 = Array.from(word1);
8-
const array2 = Array.from(word2);
7+
const chars1 = Array.from(word1);
8+
const chars2 = Array.from(word2);
99

10-
let zip = array1.flatMap((cv, i) => [cv, array2[i]]);
10+
let mergedChars = chars1.flatMap((char, index) => [char, chars2[index]]);
1111

12-
if (array2.length > array1.length) {
13-
zip = [...zip, ...array2.slice(array1.length)];
12+
if (chars2.length > chars1.length) {
13+
mergedChars = [...mergedChars, ...chars2.slice(chars1.length)];
1414
}
1515

16-
return zip.join('');
16+
return mergedChars.join('');
1717
};
1818

1919
/**
@@ -22,18 +22,18 @@ export const mergeAlternately: MergeAlternately = (word1, word2) => {
2222
export const mergeAlternately2: MergeAlternately = (word1, word2) => {
2323
let result = '';
2424

25-
let w1i = 0;
26-
let w2i = 0;
25+
let pointer1 = 0;
26+
let pointer2 = 0;
2727

28-
while (w1i < word1.length || w2i < word2.length) {
29-
if (w1i < word1.length) {
30-
result += word1[w1i];
31-
w1i += 1;
28+
while (pointer1 < word1.length || pointer2 < word2.length) {
29+
if (pointer1 < word1.length) {
30+
result += word1[pointer1];
31+
pointer1 += 1;
3232
}
3333

34-
if (w2i < word2.length) {
35-
result += word2[w2i];
36-
w2i += 1;
34+
if (pointer2 < word2.length) {
35+
result += word2[pointer2];
36+
pointer2 += 1;
3737
}
3838
}
3939

0 commit comments

Comments
 (0)