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.
Reformat the string s so that each group contains exactly k characters, except for the first group, which can be shorter than k but must still contain at least one character. There must be a dash between two consecutive groups, and all lowercase letters must be converted to uppercase.
Return the reformatted license key.
Function Signature
def format_license_key(s: str, k: int) -> str:
Rules
-
The original dashes carry no meaning. Only the letters and digits are kept, in their original order, and then regrouped.
-
The output has no leading or trailing dash.
-
If
s
contains no letters or digits (for example, it is all dashes), return the empty string.
Constraints
-
1 <= s.length <= 10^5
-
s
consists of English letters, digits, and dashes
'-'
.
-
1 <= k <= 10^4
Examples
Input: s = "5F3Z-2e-9-w", k = 4
Output: "5F3Z-2E9W"
After removing dashes and converting to uppercase, the string becomes 5F3Z2E9W, which has 8 characters. Since 8 is a multiple of 4, both groups have 4 characters.
Input: s = "2-5g-3-J", k = 2
Output: "2-5G-3J"
After removing dashes and converting to uppercase, the string becomes 25G3J. The first group has 1 character since 5 % 2 = 1, and the remaining two groups have 2 characters each.
Input: s = "---", k = 3
Output: ""
No letters or digits remain once the dashes are removed, so the result is empty.