License Key Formatting
EasyString · Apple
You are given a license key string s made of letters, digits, and dashes (-), and a positive integer group_size.
Reformat the key as follows:
- Remove every existing dash and convert all lowercase letters to uppercase.
- Split the remaining characters into groups separated by single dashes. Every group must have exactly
group_sizecharacters, except the first group, which may be shorter (but has at least one character) when the total length is not a multiple ofgroup_size.
Return the reformatted key. If s contains no letters or digits, return the empty string "".
Example 1
- Input:
- s = "a-b-c-d-e", group_size = 2
- Output:
- "A-BC-DE"
- Explanation
- Without dashes the key is
ABCDE, 5 characters. 5 is not a multiple of 2, so the leftover single character forms the first group and the rest are pairs.
Example 2
- Input:
- s = "-x9q-r2-T", group_size = 3
- Output:
- "X9Q-R2T"
- Explanation
- Removing dashes and uppercasing gives
X9QR2T. Six characters split evenly into two groups of 3.
Example 3
- Input:
- s = "---", group_size = 2
- Output:
- ""
- Explanation
- There are no letters or digits left after removing dashes, so the result is empty.
Constraints
- 1 ≤
s.length≤ 105 scontains only English letters, digits, and-- 1 ≤
group_size≤ 104