Smallest Lexicographic Label With a Given Length and Letter Sum
Last updated: November 29, 2026
Quick Overview
Letters score a=1 through z=26. Given a required label length n and a target total k, return the lexicographically smallest lowercase string of length n whose letter scores sum to k.
0
0
0 solved
Letters score a=1 through z=26. Given a required label length n and a target total k, return the lexicographically smallest lowercase string of length n whose letter scores sum to k.
A quick greedy used in Google phone screens to check you can reason about which end of a string to optimize. The interviewer expects you to fill the back with 'z' as much as possible so the front can stay 'a'.
What the Interviewer Expects
- Notice that early positions matter most for lexicographic order
- Keep front letters as small as possible
- Fill from the end with the largest letter allowed
- Leave at least 1 per remaining position
- O(n) time
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- Return the lexicographically largest such string.
- Some letters are banned.
- Add a constraint of no two equal adjacent letters.
- Validate inputs where no solution exists.
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Input: ints n and k with n <= k <= 26n. Output: smallest string of length n with letter sum k....
High-Level Design
Lexicographic order is decided by the first differing position, so we want as many leading a letters as possible and the smallest letter at the first non-a position. That means pushing the excess ...