Ctrl K

Smallest Word of a Given Weight

Medium
The weight of a lowercase English letter is its position in the alphabet: a weighs 1, b weighs 2, ..., z weighs 26. The weight of a word is the sum of its letters' weights; for example, "bad" weighs 2 + 1 + 4 = 7. Given integers length and total, return the lexicographically smallest word of length lowercase English letters whose weight is exactly total.
Example 1
length=4, total=30 → 'aabz'

1 + 1 + 2 + 26 = 30. To keep the start as small as possible, all the weight is pushed to the end.

Example 2
length=2, total=2 → 'aa'

The only two-letter word with weight 2 is "aa".

Example 3
length=3, total=78 → 'zzz'
Constraints
  • 1 ≤ length ≤ 10⁵
  • length ≤ total ≤ 26 · length
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsBiggest Number From Pieces MediumEvery Letter Once MediumErase Digits for the Smallest Number MediumSmallest Up-Down Sequence MediumRing of Flippable Words Medium
loading Python…
Input
length=4, total=30
Expected
'aabz'

Run checks the first 3 tests in your browser. Submit checks all 64 tests on the server, hidden ones included.