Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 8058526

Browse files
committed
Add solution #1754
1 parent a4df445 commit 8058526

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,521 LeetCode solutions in JavaScript
1+
# 1,522 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1351,6 +1351,7 @@
13511351
1751|[Maximum Number of Events That Can Be Attended II](./solutions/1751-maximum-number-of-events-that-can-be-attended-ii.js)|Hard|
13521352
1752|[Check if Array Is Sorted and Rotated](./solutions/1752-check-if-array-is-sorted-and-rotated.js)|Easy|
13531353
1753|[Maximum Score From Removing Stones](./solutions/1753-maximum-score-from-removing-stones.js)|Medium|
1354+
1754|[Largest Merge Of Two Strings](./solutions/1754-largest-merge-of-two-strings.js)|Medium|
13541355
1764|[Form Array by Concatenating Subarrays of Another Array](./solutions/1764-form-array-by-concatenating-subarrays-of-another-array.js)|Medium|
13551356
1765|[Map of Highest Peak](./solutions/1765-map-of-highest-peak.js)|Medium|
13561357
1768|[Merge Strings Alternately](./solutions/1768-merge-strings-alternately.js)|Easy|
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* 1754. Largest Merge Of Two Strings
3+
* https://leetcode.com/problems/largest-merge-of-two-strings/
4+
* Difficulty: Medium
5+
*
6+
* You are given two strings word1 and word2. You want to construct a string merge in the
7+
* following way: while either word1 or word2 are non-empty, choose one of the following
8+
* options:
9+
* - If word1 is non-empty, append the first character in word1 to merge and delete it from word1.
10+
* - For example, if word1 = "abc" and merge = "dv", then after choosing this operation,
11+
* word1 = "bc" and merge = "dva".
12+
* - If word2 is non-empty, append the first character in word2 to merge and delete it from word2.
13+
* - For example, if word2 = "abc" and merge = "", then after choosing this operation,
14+
* word2 = "bc" and merge = "a".
15+
*
16+
* Return the lexicographically largest merge you can construct.
17+
*
18+
* A string a is lexicographically larger than a string b (of the same length) if in the first
19+
* position where a and b differ, a has a character strictly larger than the corresponding
20+
* character in b. For example, "abcd" is lexicographically larger than "abcc" because the
21+
* first position they differ is at the fourth character, and d is greater than c.
22+
*/
23+
24+
/**
25+
* @param {string} word1
26+
* @param {string} word2
27+
* @return {string}
28+
*/
29+
var largestMerge = function(word1, word2) {
30+
let merge = '';
31+
let i = 0;
32+
let j = 0;
33+
34+
while (i < word1.length && j < word2.length) {
35+
if (word1.slice(i) >= word2.slice(j)) {
36+
merge += word1[i++];
37+
} else {
38+
merge += word2[j++];
39+
}
40+
}
41+
42+
merge += word1.slice(i) + word2.slice(j);
43+
return merge;
44+
};

0 commit comments

Comments
 (0)