|
| 1 | +# [3403. Find the Lexicographically Largest String From the Box I](https://leetcode.com/problems/find-the-lexicographically-largest-string-from-the-box-i) |
| 2 | + |
| 3 | +## Description |
| 4 | + |
| 5 | +<div class="elfjS" data-track-load="description_content"><p>You are given a string <code>word</code>, and an integer <code>numFriends</code>.</p> |
| 6 | + |
| 7 | +<p>Alice is organizing a game for her <code>numFriends</code> friends. There are multiple rounds in the game, where in each round:</p> |
| 8 | + |
| 9 | +<ul> |
| 10 | + <li><code>word</code> is split into <code>numFriends</code> <strong>non-empty</strong> strings, such that no previous round has had the <strong>exact</strong> same split.</li> |
| 11 | + <li>All the split words are put into a box.</li> |
| 12 | +</ul> |
| 13 | + |
| 14 | +<p>Find the <span data-keyword="lexicographically-smaller-string" class=" cursor-pointer relative text-dark-blue-s text-sm"><button type="button" aria-haspopup="dialog" aria-expanded="false" aria-controls="radix-:rj:" data-state="closed" class="">lexicographically largest</button></span> string from the box after all the rounds are finished.</p> |
| 15 | + |
| 16 | +<p> </p> |
| 17 | +<p><strong class="example">Example 1:</strong></p> |
| 18 | + |
| 19 | +<div class="example-block"> |
| 20 | +<p><strong>Input:</strong> <span class="example-io">word = "dbca", numFriends = 2</span></p> |
| 21 | + |
| 22 | +<p><strong>Output:</strong> <span class="example-io">"dbc"</span></p> |
| 23 | + |
| 24 | +<p><strong>Explanation:</strong> </p> |
| 25 | + |
| 26 | +<p>All possible splits are:</p> |
| 27 | + |
| 28 | +<ul> |
| 29 | + <li><code>"d"</code> and <code>"bca"</code>.</li> |
| 30 | + <li><code>"db"</code> and <code>"ca"</code>.</li> |
| 31 | + <li><code>"dbc"</code> and <code>"a"</code>.</li> |
| 32 | +</ul> |
| 33 | +</div> |
| 34 | + |
| 35 | +<p><strong class="example">Example 2:</strong></p> |
| 36 | + |
| 37 | +<div class="example-block"> |
| 38 | +<p><strong>Input:</strong> <span class="example-io">word = "gggg", numFriends = 4</span></p> |
| 39 | + |
| 40 | +<p><strong>Output:</strong> <span class="example-io">"g"</span></p> |
| 41 | + |
| 42 | +<p><strong>Explanation:</strong> </p> |
| 43 | + |
| 44 | +<p>The only possible split is: <code>"g"</code>, <code>"g"</code>, <code>"g"</code>, and <code>"g"</code>.</p> |
| 45 | +</div> |
| 46 | + |
| 47 | +<p> </p> |
| 48 | +<p><strong>Constraints:</strong></p> |
| 49 | + |
| 50 | +<ul> |
| 51 | + <li><code>1 <= word.length <= 5 * 10<sup>3</sup></code></li> |
| 52 | + <li><code>word</code> consists only of lowercase English letters.</li> |
| 53 | + <li><code>1 <= numFriends <= word.length</code></li> |
| 54 | +</ul> |
| 55 | +</div> |
| 56 | + |
| 57 | +<p> </p> |
| 58 | + |
| 59 | +## Solutions |
| 60 | + |
| 61 | +**Solution: `Greedy`** |
| 62 | + |
| 63 | +- Time complexity: <em>O(n<sup>2</sup>)</em> |
| 64 | +- Space complexity: <em>O(n)</em> |
| 65 | + |
| 66 | +<p> </p> |
| 67 | + |
| 68 | +### **JavaScript** |
| 69 | + |
| 70 | +```js |
| 71 | +/** |
| 72 | + * @param {string} word |
| 73 | + * @param {number} numFriends |
| 74 | + * @return {string} |
| 75 | + */ |
| 76 | +const answerString = function (word, numFriends) { |
| 77 | + if (numFriends === 1) return word; |
| 78 | + const n = word.length; |
| 79 | + let result = ''; |
| 80 | + |
| 81 | + for (let index = 0; index < n; index++) { |
| 82 | + if (result[0] > word[index]) continue; |
| 83 | + const needSplit = Math.max(numFriends - index - 1, 0); |
| 84 | + const splitWord = word.slice(index, n - needSplit); |
| 85 | + |
| 86 | + if (splitWord > result) { |
| 87 | + result = splitWord; |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + return result; |
| 92 | +}; |
| 93 | +``` |
0 commit comments