Problem
You are given a license key represented as a string s
that consists of only alphanumeric characters and dashes. The string is separated into n + 1
groups by n
dashes. You are also given an integer k
.
We want to reformat the string s
such that each group contains exactly k
characters, except for the first group, which could be shorter than k
but still must contain at least one character. Furthermore, there must be a dash inserted between two groups, and you should convert all lowercase letters to uppercase.
Return the reformatted license key.
Examples
Example 1:
Input: s = “5F3Z-2e-9-w”, k = 4 Output: “5F3Z-2E9W” Explanation: The string s has been split into two parts, each part has 4 characters. Note that the two extra dashes are not needed and can be removed.
Example 2:
Input: s = “2-5g-3-J”, k = 2 Output: “2-5G-3J” Explanation: The string s has been split into three parts, each part has 2 characters except the first part as it could be shorter as mentioned above.
Solution
Method 1 – Reverse and Greedy Grouping
Intuition
The main idea is to ignore dashes, convert all letters to uppercase, and then group the characters from the end in chunks of size k
. This way, the first group can be shorter, and the rest are exactly k
in length.
Approach
- Remove all dashes and convert all letters to uppercase.
- Start from the end of the string and collect groups of size
k
. - Join the groups with dashes, reversing at the end to restore the correct order.
Code
|
|
|
|
Complexity
- ⏰ Time complexity:
O(n)
— Each character is processed a constant number of times. - 🧺 Space complexity:
O(n)
— For the cleaned string and the answer.
Method 2 – Forward Grouping with Modulo
Intuition
By removing dashes and uppercasing, we can process the string from the start, placing a dash before every group except the first, using the modulo of the current index to determine when to insert a dash.
Approach
- Remove all dashes and convert to uppercase.
- Calculate the length of the first group:
len(s) % k
(unless it’s zero, then it’sk
). - Build the result by adding the first group, then every next group of size
k
with a dash before each.
Code
|
|
|
|
Complexity
- ⏰ Time complexity:
O(n)
— Each character is processed a constant number of times. - 🧺 Space complexity:
O(n)
— For the cleaned string and the answer.