dsa · easy
License Key Formatting
s is a license string of English letters, digits, and dashes. Reformat it:
Arguments
s— raw key of letters, digits, and dashesk— size of every group except possibly the leftmost leftover
- drop every existing dash
- uppercase every letter
- regroup the remaining characters into groups of exactly
kfrom the **right**; the leftmost group may be shorter, but it must not be empty if any characters remain - insert a single dash between groups
If nothing but dashes remains, return an empty string.
Example
s = "5F3Z-2e-9-w", k = 4. Stripping dashes and uppercasing gives 5F3Z2E9W (8 characters). Two groups of 4 from the right: "5F3Z-2E9W".
s = "2-5g-3-J", k = 2 → characters 25G3J. Left leftover 2, then pairs → "2-5G-3J".
Constraints
1 <= s.length <= 10^5 1 <= k <= 10^4 s contains only English letters, digits, and dashes Hidden tests include near-max size for this bound; a slower-than-intended solution TLEs.
Examples
Example 1
Input: '5F3Z-2e-9-w' 4 Expected: 5F3Z-2E9W
Example 2
Input: '2-5g-3-J' 2 Expected: 2-5G-3J