Skip to content

Commit 0c479f6

Browse files
add problem 1768
1 parent 0bdc7c6 commit 0c479f6

File tree

4 files changed

+60
-2
lines changed

4 files changed

+60
-2
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
## Problem
2+
3+
You are given two strings word1 and word2. Merge the strings by adding letters in alternating order, starting with word1. If a string is longer than the other, append the additional letters onto the end of the merged string.
4+
5+
Return the merged string.
6+
7+
Example 1:
8+
9+
Input: word1 = "abc", word2 = "pqr"
10+
Output: "apbqcr"
11+
Explanation: The merged string will be merged as so:
12+
word1: a b c
13+
word2: p q r
14+
merged: a p b q c r
15+
16+
Example 2:
17+
18+
Input: word1 = "ab", word2 = "pqrs"
19+
Output: "apbqrs"
20+
Explanation: Notice that as word2 is longer, "rs" is appended to the end.
21+
word1: a b
22+
word2: p q r s
23+
merged: a p b q r s
24+
25+
Example 3:
26+
27+
Input: word1 = "abcd", word2 = "pq"
28+
Output: "apbqcd"
29+
Explanation: Notice that as word1 is longer, "cd" is appended to the end.
30+
word1: a b c d
31+
word2: p q
32+
merged: a p b q c d
33+
34+
Constraints:
35+
36+
1 <= word1.length, word2.length <= 100
37+
word1 and word2 consist of lowercase English letters.
38+
39+
## Pre analysis
40+
41+
This problem is mostly aligned with merge phase of merge sort. You push alternatively each element into result until you run out of elements from both
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* @param {string} word1
3+
* @param {string} word2
4+
* @return {string}
5+
*/
6+
var mergeAlternately = function (word1, word2) {
7+
let result = [];
8+
const max = Math.max(word1.length, word2.length);
9+
10+
for (let i = 0; i < max; i++) {
11+
if (i < word1.length) result.push(word1[i]);
12+
if (i < word2.length) result.push(word2[i]);
13+
}
14+
return result.join("");
15+
};

Easy/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
## Problems Solved - 227
1+
## Problems Solved - 228
22

33
| # | Problem Name | Difficulty | Solution |
44
| ---- | ------------ | ---------- | ----------- |
@@ -140,6 +140,7 @@
140140
| <a href="/Easy/1688 Count of Matches in Tournament/">1688</a> | <a href="/Easy/1688 Count of Matches in Tournament">Count of Matches in Tournament</a>| Easy | <a href="/Easy/1688 Count of Matches in Tournament/countOfMatchesInTournament.js">Solution</a> |
141141
| <a href="/Easy/1732 Find the Highest Altitude/">1732</a> | <a href="/Easy/1732 Find the Highest Altitude">Find the Highest Altitude</a>| Easy | <a href="/Easy/1732 Find the Highest Altitude/findTheHighestAltitude.js">Solution</a> |
142142
| <a href="/Easy/1748 Sum of Unique Elements/">1748</a> | <a href="/Easy/1748 Sum of Unique Elements">Sum of Unique Elements</a>| Easy | <a href="/Easy/1748 Sum of Unique Elements/sumOfUniqueElements.js">Solution</a> |
143+
| <a href="/Easy/1768 Merge Strings Alternately/">1768</a> | <a href="/Easy/1768 Merge Strings Alternately">Merge Strings Alternately</a>| Easy | <a href="/Easy/1768 Merge Strings Alternately/mergeStringsAlternately.js">Solution</a> |
143144
| <a href="/Easy/1773 Count Items Matching a Rule/">1773</a> | <a href="/Easy/1773 Count Items Matching a Rule">Count Items Matching a Rule</a>| Easy | <a href="/Easy/1773 Count Items Matching a Rule/countItemsMatchingARule.js">Solution</a> |
144145
| <a href="/Easy/1812 Determine Color of a Chessboard Square/">1812</a> | <a href="/Easy/1812 Determine Color of a Chessboard Square">Determine Color of a Chessboard Square</a>| Easy | <a href="/Easy/1812 Determine Color of a Chessboard Square/determineColorOfAChessboardSquare.js">Solution</a> |
145146
| <a href="/Easy/1816 Truncate Sentence/">1816</a> | <a href="/Easy/1816 Truncate Sentence">Truncate Sentence</a>| Easy | <a href="/Easy/1816 Truncate Sentence/truncateSentence.js">Solution</a> |

Problems.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
## Problems Solved - 269
1+
## Problems Solved - 270
22

33
| # | Problem Name | Difficulty | Solution |
44
| ---- | ------------ | ---------- | ----------- |
@@ -168,6 +168,7 @@
168168
| <a href="/Easy/1688 Count of Matches in Tournament/">1688</a> | <a href="/Easy/1688 Count of Matches in Tournament">Count of Matches in Tournament</a>| Easy | <a href="/Easy/1688 Count of Matches in Tournament/countOfMatchesInTournament.js">Solution</a> |
169169
| <a href="/Easy/1732 Find the Highest Altitude/">1732</a> | <a href="/Easy/1732 Find the Highest Altitude">Find the Highest Altitude</a>| Easy | <a href="/Easy/1732 Find the Highest Altitude/findTheHighestAltitude.js">Solution</a> |
170170
| <a href="/Easy/1748 Sum of Unique Elements/">1748</a> | <a href="/Easy/1748 Sum of Unique Elements">Sum of Unique Elements</a>| Easy | <a href="/Easy/1748 Sum of Unique Elements/sumOfUniqueElements.js">Solution</a> |
171+
| <a href="/Easy/1768 Merge Strings Alternately/">1768</a> | <a href="/Easy/1768 Merge Strings Alternately">Merge Strings Alternately</a>| Easy | <a href="/Easy/1768 Merge Strings Alternately/mergeStringsAlternately.js">Solution</a> |
171172
| <a href="/Easy/1773 Count Items Matching a Rule/">1773</a> | <a href="/Easy/1773 Count Items Matching a Rule">Count Items Matching a Rule</a>| Easy | <a href="/Easy/1773 Count Items Matching a Rule/countItemsMatchingARule.js">Solution</a> |
172173
| <a href="/Easy/1812 Determine Color of a Chessboard Square/">1812</a> | <a href="/Easy/1812 Determine Color of a Chessboard Square">Determine Color of a Chessboard Square</a>| Easy | <a href="/Easy/1812 Determine Color of a Chessboard Square/determineColorOfAChessboardSquare.js">Solution</a> |
173174
| <a href="/Easy/1816 Truncate Sentence/">1816</a> | <a href="/Easy/1816 Truncate Sentence">Truncate Sentence</a>| Easy | <a href="/Easy/1816 Truncate Sentence/truncateSentence.js">Solution</a> |

0 commit comments

Comments
 (0)