↓Skip to main content
  1. LeetCode/

LeetCode 482: License Key Formatting

·2 mins· ·
LeetCode Easy String
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: easy First Attempt: 2022-08-24

  • Submissions: Wrong Answer ×8 → Output Limit Exceeded → Accepted

Approach
#

Only the first group may be shorter, so cut groups of k from the end. Remove every -, uppercase, take a first group of length len % k (or k when that is 0), then groups of k, joined with "-".join (String Template 1).

My Attempts
#

This took 10 submissions, all on counting “a - every k characters from the back”:

  • The first four did not uppercase: "a" returned "a" instead of "A".
  • With upper() added, the counter started at 1 and used c <= k, making groups of k + 1: "B2a", k = 1 should be "B-2-A"; it returned "B2-A".
  • Switching to c < k sent the very first character into the else when k = 1, adding a stray -: "2" returned "2-".
  • Starting the counter at 0 fixed it, but a leftover print caused Output Limit Exceeded; removing it passed.

Solution
#

class Solution:
    def licenseKeyFormatting(self, s: str, k: int) -> str:
        t = s.replace("-", "").upper()
        first = len(t) % k or k
        groups = [t[:first]] + [t[i:i + k] for i in range(first, len(t), k)]
        return "-".join(g for g in groups if g)

Complexity
#

Time O(n), space O(n).

Mistakes to Avoid
#

  • Uppercase the result.
  • Groups hold k characters; when counting from the back, the counter’s start value and comparison must agree, or groups are off by one or a leading - appears.
  • When s is all dashes the answer is empty.

Interview Explanation
#

I strip dashes and uppercase, make the first group len % k long (or k when that is 0), cut the rest into groups of k, and join them with dashes. Time O(n).

Related

LeetCode 1446: Consecutive Characters
·1 min
LeetCode Easy String
LeetCode 1446 note: track the current run of equal characters in one pass, resetting to 1 on a change
LeetCode 1869: Longer Contiguous Segments of Ones than Zeros
·1 min
LeetCode Easy String
LeetCode 1869 note: find the longest run of 1s and of 0s and compare; split does it in one line
LeetCode 415: Add Strings
·2 mins
LeetCode Easy Math String Simulation
LeetCode 415 note: add digit by digit from the ends with two pointers, tracking the carry, and append any leftover carry after the loop
LeetCode 459: Repeated Substring Pattern
·1 min
LeetCode Easy String String-Matching Z-Algorithm Knuth-Morris-Pratt-Algorithm
LeetCode 459 note: s is a repetition of a substring exactly when s occurs in s + s with its first and last characters removed
LeetCode 290: Word Pattern
·1 min
LeetCode Easy Hash-Table String
LeetCode 290 note: pattern characters and words must correspond one to one; use two dicts or compare set sizes
LeetCode 344: Reverse String
·1 min
LeetCode Easy Two-Pointers String
LeetCode 344 note: swap with two pointers and move inward to reverse in place