Skip to content

Commit 16f7fa7

Browse files
committed
Add solution #3406
1 parent 441383a commit 16f7fa7

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2688,6 +2688,7 @@
26882688
3402|[Minimum Operations to Make Columns Strictly Increasing](./solutions/3402-minimum-operations-to-make-columns-strictly-increasing.js)|Easy|
26892689
3403|[Find the Lexicographically Largest String From the Box I](./solutions/3403-find-the-lexicographically-largest-string-from-the-box-i.js)|Medium|
26902690
3405|[Count the Number of Arrays with K Matching Adjacent Elements](./solutions/3405-count-the-number-of-arrays-with-k-matching-adjacent-elements.js)|Hard|
2691+
3406|[Find the Lexicographically Largest String From the Box II](./solutions/3406-find-the-lexicographically-largest-string-from-the-box-ii.js)|Hard|
26912692
3423|[Maximum Difference Between Adjacent Elements in a Circular Array](./solutions/3423-maximum-difference-between-adjacent-elements-in-a-circular-array.js)|Easy|
26922693
3439|[Reschedule Meetings for Maximum Free Time I](./solutions/3439-reschedule-meetings-for-maximum-free-time-i.js)|Medium|
26932694
3440|[Reschedule Meetings for Maximum Free Time II](./solutions/3440-reschedule-meetings-for-maximum-free-time-ii.js)|Medium|
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* 3406. Find the Lexicographically Largest String From the Box II
3+
* https://leetcode.com/problems/find-the-lexicographically-largest-string-from-the-box-ii/
4+
* Difficulty: Hard
5+
*
6+
* You are given a string word, and an integer numFriends.
7+
*
8+
* Alice is organizing a game for her numFriends friends. There are multiple rounds in the game,
9+
* where in each round:
10+
* - word is split into numFriends non-empty strings, such that no previous round has had the
11+
* exact same split.
12+
* - All the split words are put into a box.
13+
*
14+
* Find the lexicographically largest string from the box after all the rounds are finished.
15+
*
16+
* A string a is lexicographically smaller than a string b if in the first position where a
17+
* and b differ, string a has a letter that appears earlier in the alphabet than the corresponding
18+
* letter in b.
19+
*
20+
* If the first min(a.length, b.length) characters do not differ, then the shorter string is the
21+
* lexicographically smaller one.
22+
*/
23+
24+
/**
25+
* @param {string} word
26+
* @param {number} numFriends
27+
* @return {string}
28+
*/
29+
var answerString = function(word, numFriends) {
30+
if (numFriends === 1) return word;
31+
const n = word.length;
32+
const maxLength = n - numFriends + 1;
33+
34+
const pairs = [];
35+
for (let i = 0; i < n - 1; i++) {
36+
pairs.push(word[i] + word[i + 1]);
37+
}
38+
const maxPair = pairs.reduce((a, b) => a > b ? a : b);
39+
let result = word[n - 1];
40+
let index = -1;
41+
42+
while (index < n) {
43+
index = word.indexOf(maxPair, index + 1);
44+
if (index === -1) break;
45+
const candidate = word.substring(index, index + maxLength);
46+
if (candidate > result) {
47+
result = candidate;
48+
}
49+
}
50+
51+
return result;
52+
};

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy