Find the Lexicographically Largest String From the Box I - Solution & Explanation
Problem Statement
You are given a string word, and an integer numFriends.
Alice is organizing a game for her numFriends friends. There are multiple rounds in the game, where in each round:
wordis split intonumFriendsnon-empty strings, such that no previous round has had the exact same split.- All the split words are put into a box.
Find the lexicographically largest string from the box after all the rounds are finished.
Example 1:
Input: word = "dbca", numFriends = 2
Output: "dbc"
Explanation:
All possible splits are:
"d"and"bca"."db"and"ca"."dbc"and"a".
Example 2:
Input: word = "gggg", numFriends = 4
Output: "g"
Explanation:
The only possible split is: "g", "g", "g", and "g".
Constraints:
1 <= word.length <= 5 * 103wordconsists only of lowercase English letters.1 <= numFriends <= word.length
Approach Overview
Problem Overview: You are given a string s and an integer k. The string is split into k non‑empty substrings. Among all substrings placed in the box, return the lexicographically largest possible string that could appear.
Approach 1: Enumerate Substring Left Endpoints (O(n²) time, O(1) extra space)
The key constraint is that all k substrings must be non‑empty. If a substring starts at index i, the remaining characters must still be enough to form the other k-1 substrings. That limits the maximum length of the current substring to n - i - (k - 1). Iterate through every possible starting index and construct the longest valid substring beginning there. Compare each candidate using standard lexicographical comparison and keep the maximum.
This works because a longer prefix with a larger leading character dominates lexicographically. By always taking the maximum allowed length for each start index, you ensure the candidate is the best possible substring beginning at that position. The algorithm is straightforward: iterate through the string, compute the valid substring length, extract the candidate, and update the best answer if it is larger.
Although substring comparison can cost up to O(n), the total complexity remains O(n²) in the worst case, which is acceptable for typical constraints in medium string problems. The implementation relies entirely on basic string operations and simple iteration.
This problem mainly tests understanding of string comparison rules and careful boundary calculation while enumerating candidates. Some implementations also optimize comparisons using two pointers to avoid unnecessary substring creation, but the core idea remains enumerating valid starting points.
Recommended for interviews: Enumerating substring start positions is the expected approach. A brute-force mindset shows you understand the search space, but correctly limiting substring length using the k constraint demonstrates stronger reasoning about problem constraints and lexicographical ordering.
Solution
If we fix the left endpoint of the substring, the longer the substring, the larger its lexicographical order. Suppose the left endpoint of the substring is i, and the minimum length of the remaining substrings is numFriends - 1, then the right endpoint of the substring can be up to min(n, i + n - (numFriends - 1)), where n is the length of the string. Note that we are talking about left-closed, right-open intervals.
We enumerate all possible left endpoints, extract the corresponding substrings, compare their lexicographical order, and finally obtain the lexicographically largest substring.
The time complexity is O(n^2), and the space complexity is O(n), where n is the length of the string.
Code
Python
Java
C++
Go
TypeScript
Detailed Complexity Analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Enumerate Substring Left Endpoints | O(n²) | O(1) | General case. Simple and reliable for typical string constraints. |
| Enumeration with Two-Pointer Comparison | O(n²) | O(1) | When avoiding substring allocations and comparing characters directly. |
Video Solution
Find the Lexicographically Largest String From the Box I | Thought Process | Leetcode 3403 | MIK • codestorywithMIK • 11,029 views views
Watch 9 more video solutions →Frequently Asked Questions
Is Find the Lexicographically Largest String From the Box I easy or hard?
Find the Lexicographically Largest String From the Box I Python/Java solution
How to solve Find the Lexicographically Largest String From the Box I in O(n)?
What is the best approach for Find the Lexicographically Largest String From the Box I?
Is Find the Lexicographically Largest String From the Box I asked at Google/Amazon/Meta?
What data structure is used in Find the Lexicographically Largest String From the Box I?
What is the time complexity of Find the Lexicographically Largest String From the Box I?
Ready to solve this problem?
Practice Find the Lexicographically Largest String From the Box I with our built-in code editor and test cases.
Practice on FleetCode