Skip to main content
Back to problems
Leetcode
Easy
Strings
Arrays
License Key Formatting

Reformat a license key string into groups of fixed size separated by dashes, with letters in uppercase.

Acceptance 100%
Problem Statement

License Key Formatting

You are given a string representing an existing license key. It contains letters, digits, and dash characters (-). Your task is to reformat it so that:

  • All dashes are removed from the original string.
  • All letters are converted to uppercase.
  • The resulting string is split into groups separated by dashes.
  • The first group may be shorter than the others, but every later group must contain exactly k characters.

Return the reformatted license key.

This is a straightforward string-processing problem: normalize the characters first, then rebuild the formatted output from right to left or left to right while respecting the group size.

Input Format

  • A string s containing alphanumeric characters and dashes.
  • An integer k, the required group size for all groups except possibly the first.

Output Format

  • Return a single string representing the reformatted license key.

Constraints

  • Letters should be uppercase in the output.
  • Dashes in the input should not appear except as separators in the formatted output.
  • Group size for all groups except the first is exactly k.
  • Input may contain only dashes besides alphanumeric characters.
Examples
Sample cases returned by the problem API.

Example 1

Input

s = "5F3Z-2e-9-w", k = 4

Output

"5F3Z-2E9W"

Explanation

After removing dashes and converting to uppercase, the string becomes 5F3Z2E9W. With k = 4, it is already one group of length 4 followed by another group of length 4, so the result is 5F3Z-2E9W.

Example 2

Input

s = "2-5g-3-J", k = 2

Output

"2-5G-3J"

Explanation

Normalize to 25G3J. Grouping from the end with size 2 gives 2-5G-3J.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.