JuniorString

License Key Formatting

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.
  • The first group may be shorter than k, but must still contain at least one character.
  • A dash must be inserted between every two groups.
  • All lowercase letters must be converted to uppercase.

Return the reformatted license key.

Example 1
Inputs = "5F3Z-2e-9-w", k = 4
Output"5F3Z-2E9W"
The string s has been split into two parts, each with 4 characters, and the extra dashes are removed.
Example 2
Inputs = "2-5g-3-J", k = 2
Output"2-5G-3J"
The string s has been split into three parts, each with 2 characters except the first part, which is shorter.

Constraints

  • 1 <= s.length <= 10^5
  • s consists of English letters, digits, and dashes '-'
  • 1 <= k <= 10^4

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate