License Key Formatting - Problem
Transform a messy license key into a perfectly formatted one! You're given a license key string containing alphanumeric characters and dashes scattered throughout. Your mission is to reformat this string so that:
- Each group contains exactly k characters (except the first group which can be shorter)
- Groups are separated by dashes
- All letters are converted to uppercase
- The first group must contain at least one character
"5F3Z-2e-9-w" with k=4 becomes "5F3Z-2E9W"
Think of it like reorganizing a messy product serial number into a clean, standardized format that's easy to read and type! Input & Output
example_1.py โ Basic formatting
$
Input:
s = "5F3Z-2e-9-w", k = 4
โบ
Output:
"5F3Z-2E9W"
๐ก Note:
The string has 8 valid characters. First group gets 4 characters (5F3Z), second group gets the remaining 4 characters (2E9W), separated by a dash.
example_2.py โ Shorter first group
$
Input:
s = "2-5g-3-J", k = 2
โบ
Output:
"2-5G-3J"
๐ก Note:
5 valid characters total. First group gets 1 character (2), then two groups of 2 characters each (5G, 3J).
example_3.py โ No dashes needed
$
Input:
s = "2-4A0r7-4k", k = 4
โบ
Output:
"24A0R74K"
๐ก Note:
8 valid characters fit exactly into 2 groups of 4, but since we want first group to be shorter if needed, all 8 characters form one group less than k*2.
Constraints
- 1 โค s.length โค 3 ร 104
- s consists of English letters, digits, and dashes '-'
- 1 โค k โค 104
- First group can be shorter than k but must contain at least one character
Visualization
Tap to expand
Understanding the Visualization
1
Input Cleaning
Remove all dashes and convert letters to uppercase
2
Reverse Grouping
Process from right to left, creating groups of k characters
3
Final Assembly
Reverse the result to get proper order with dashes between groups
Key Takeaway
๐ฏ Key Insight: Working backwards naturally ensures all groups except the first have exactly k characters, eliminating complex group size calculations!
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code