Skip to content

Commit b0bdfb9

Browse files
committed
Sync LeetCode submission Runtime - 23 ms (29.55%), Memory - 18.1 MB (32.58%)
1 parent f6df1c1 commit b0bdfb9

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<p>You are given a string <code>word</code>, and an integer <code>numFriends</code>.</p>
2+
3+
<p>Alice is organizing a game for her <code>numFriends</code> friends. There are multiple rounds in the game, where in each round:</p>
4+
5+
<ul>
6+
<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>
7+
<li>All the split words are put into a box.</li>
8+
</ul>
9+
10+
<p>Find the <span data-keyword="lexicographically-smaller-string">lexicographically largest</span> string from the box after all the rounds are finished.</p>
11+
12+
<p>&nbsp;</p>
13+
<p><strong class="example">Example 1:</strong></p>
14+
15+
<div class="example-block">
16+
<p><strong>Input:</strong> <span class="example-io">word = &quot;dbca&quot;, numFriends = 2</span></p>
17+
18+
<p><strong>Output:</strong> <span class="example-io">&quot;dbc&quot;</span></p>
19+
20+
<p><strong>Explanation:</strong>&nbsp;</p>
21+
22+
<p>All possible splits are:</p>
23+
24+
<ul>
25+
<li><code>&quot;d&quot;</code> and <code>&quot;bca&quot;</code>.</li>
26+
<li><code>&quot;db&quot;</code> and <code>&quot;ca&quot;</code>.</li>
27+
<li><code>&quot;dbc&quot;</code> and <code>&quot;a&quot;</code>.</li>
28+
</ul>
29+
</div>
30+
31+
<p><strong class="example">Example 2:</strong></p>
32+
33+
<div class="example-block">
34+
<p><strong>Input:</strong> <span class="example-io">word = &quot;gggg&quot;, numFriends = 4</span></p>
35+
36+
<p><strong>Output:</strong> <span class="example-io">&quot;g&quot;</span></p>
37+
38+
<p><strong>Explanation:</strong>&nbsp;</p>
39+
40+
<p>The only possible split is: <code>&quot;g&quot;</code>, <code>&quot;g&quot;</code>, <code>&quot;g&quot;</code>, and <code>&quot;g&quot;</code>.</p>
41+
</div>
42+
43+
<p>&nbsp;</p>
44+
<p><strong>Constraints:</strong></p>
45+
46+
<ul>
47+
<li><code>1 &lt;= word.length &lt;= 5&nbsp;* 10<sup>3</sup></code></li>
48+
<li><code>word</code> consists only of lowercase English letters.</li>
49+
<li><code>1 &lt;= numFriends &lt;= word.length</code></li>
50+
</ul>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Approach 1: Enumeration
2+
3+
# Time: O(n^2)
4+
# Space: O(1)
5+
6+
class Solution:
7+
def answerString(self, word: str, numFriends: int) -> str:
8+
if numFriends == 1:
9+
return word
10+
11+
n = len(word)
12+
res = ""
13+
for i in range(n):
14+
res = max(res, word[i : min(i + n - numFriends + 1, n)])
15+
16+
return res
17+

0 commit comments

Comments
 (0)