Skip to content

Commit

Permalink
254th Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Shyam-Chen committed Dec 22, 2024
1 parent 0c820c1 commit 6c5fe12
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 26 deletions.
23 changes: 12 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,20 @@ Ace Coding Interview with 75 Qs
<details>
<summary>Problems</summary>

| Array / String | | |
| ---------------------------------------------- | ---------------- | ------ |
| 1768. Merge Strings Alternately | [Solution][1768] | Easy |
| 1071. Greatest Common Divisor of Strings | [Solution][1071] | Easy |
| 1431. Kids With the Greatest Number of Candies | [Solution][1431] | Easy |
| 605. Can Place Flowers | [Solution][605] | Easy |
| 345. Reverse Vowels of a String | [Solution][345] | Easy |
| 151. Reverse Words in a String | [Solution][151] | Medium |
| 238. Product of Array Except Self | [Solution][238] | Medium |
| 334. Increasing Triplet Subsequence | [Solution][334] | Medium |
| 443. String Compression | [Solution][443] | Medium |
| Array / String | | | |
| ---------------------------------------------- | ---------------- | ------ | ------------------------ |
| 1768. Merge Strings Alternately | [Solution][1768] | Easy | [詳解][1768-explanation] |
| 1071. Greatest Common Divisor of Strings | [Solution][1071] | Easy | 詳解 |
| 1431. Kids With the Greatest Number of Candies | [Solution][1431] | Easy | 詳解 |
| 605. Can Place Flowers | [Solution][605] | Easy | 詳解 |
| 345. Reverse Vowels of a String | [Solution][345] | Easy | 詳解 |
| 151. Reverse Words in a String | [Solution][151] | Medium | 詳解 |
| 238. Product of Array Except Self | [Solution][238] | Medium | 詳解 |
| 334. Increasing Triplet Subsequence | [Solution][334] | Medium | 詳解 |
| 443. String Compression | [Solution][443] | Medium | 詳解 |

[1768]: ./src/page-17/1768.%20Merge%20Strings%20Alternately/mergeAlternately.ts
[1768-explanation]: ./src/page-17/1768.%20Merge%20Strings%20Alternately/README.md
[1071]: ./src/page-11/1071.%20Greatest%20Common%20Divisor%20of%20Strings/gcdOfStrings.ts
[1431]: ./src/page-14/1431.%20Kids%20With%20the%20Greatest%20Number%20of%20Candies/kidsWithCandies.ts
[605]: ./src/page-6/605.%20Can%20Place%20Flowers/canPlaceFlowers.ts
Expand Down
87 changes: 87 additions & 0 deletions src/page-17/1768. Merge Strings Alternately/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# 1768. 交替合併字串 (Merge Strings Alternately)

給你兩個字串 `word1``word2`。請你從 `word1` 開始,透過交替加上字母來合併字串。如果一個字串比另一個字串長,就將多出來的字母追加到合併後字串的結尾。

返回*合併後的字串*

範例 1:

```ts
輸入: word1 = "abc", word2 = "pqr"
輸出: "apbqcr"
說明: 字串合併情況如下所示:
word1: a b c
word2: p q r
合併後: a p b q c r
```

範例 2:

```ts
輸入: word1 = "ab", word2 = "pqrs"
輸出: "apbqrs"
說明: 注意,word2word1 長,"rs" 需要追加到合併後字串的結尾。
word1: a b
word2: p q r s
合併後: a p b q r s
```

範例 3:

```ts
輸入: word1 = "abcd", word2 = "pq"
輸出: "apbqcd"
說明: 注意,word1word2 長,"cd" 需要追加到合併後字串的結尾。
word1: a b c d
word2: p q
合併後: a p b q c d
```

## 解題

```ts
export const mergeAlternately: MergeAlternately = (word1, word2) => {
const chars1 = Array.from(word1);
const chars2 = Array.from(word2);

// 使用 flatMap 方法,將兩個字串的字元交替合併
let mergedChars = chars1.flatMap((char, index) => [char, chars2[index]]);

// 如果 chars2 比 chars1 長,說明還有剩下的字元需要附加到結果陣列中
if (chars2.length > chars1.length) {
mergedChars = [...mergedChars, ...chars2.slice(chars1.length)];
}

return mergedChars.join('');
};
```

## 解題 2

使用雙指標 (Two Pointers)

```ts
export const mergeAlternately2: MergeAlternately = (word1, word2) => {
let result = ''; // 儲存合併後的結果

let pointer1 = 0; // 指向 word1 的當前位置
let pointer2 = 0; // 指向 word2 的當前位置

// 當兩個指標都還在有效範圍時,交替加入字元
while (pointer1 < word1.length || pointer2 < word2.length) {
if (pointer1 < word1.length) {
// 從 word1 中取一個字元並加入結果,然後遞增指標
result += word1[pointer1];
pointer1 += 1;
}

if (pointer2 < word2.length) {
// 從 word2 中取一個字元並加入結果,然後遞增指標
result += word2[pointer2];
pointer2 += 1;
}
}

return result;
};
```
30 changes: 15 additions & 15 deletions src/page-17/1768. Merge Strings Alternately/mergeAlternately.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ type MergeAlternately = (word1: string, word2: string) => string;
* Accepted
*/
export const mergeAlternately: MergeAlternately = (word1, word2) => {
const array1 = Array.from(word1);
const array2 = Array.from(word2);
const chars1 = Array.from(word1);
const chars2 = Array.from(word2);

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

if (array2.length > array1.length) {
zip = [...zip, ...array2.slice(array1.length)];
if (chars2.length > chars1.length) {
mergedChars = [...mergedChars, ...chars2.slice(chars1.length)];
}

return zip.join('');
return mergedChars.join('');
};

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

let w1i = 0;
let w2i = 0;
let pointer1 = 0;
let pointer2 = 0;

while (w1i < word1.length || w2i < word2.length) {
if (w1i < word1.length) {
result += word1[w1i];
w1i += 1;
while (pointer1 < word1.length || pointer2 < word2.length) {
if (pointer1 < word1.length) {
result += word1[pointer1];
pointer1 += 1;
}

if (w2i < word2.length) {
result += word2[w2i];
w2i += 1;
if (pointer2 < word2.length) {
result += word2[pointer2];
pointer2 += 1;
}
}

Expand Down

0 comments on commit 6c5fe12

Please sign in to comment.