|
4 | 4 | * [Crop String](#crop-string) |
5 | 5 | * [First Unique Character](#first-unique-character-in-a-string) |
6 | 6 | * [Jewels and Stones](#jewels-and-stones) |
| 7 | +* [License Key Formatter](#license-key-formatter) |
7 | 8 | * [Longest Common Prefix](#longest-common-prefix) |
8 | 9 | * [Longest Substring Without Duplicates](#longest-substring-without-duplicates) |
9 | 10 | * [Multiply Strings](#multiply-strings) |
@@ -174,6 +175,37 @@ def jewels_and_stones(jewels: str, stones: str) -> int: |
174 | 175 | return count |
175 | 176 | ``` |
176 | 177 |
|
| 178 | +## License Key Formatter |
| 179 | + |
| 180 | +Given a license string which consists only alphanumeric character and dashes. |
| 181 | +The string is separated into `N+1` groups by `N` dashes. |
| 182 | + |
| 183 | +Given a `group_length` reformat the strings such that each group contains the exact number of characters, |
| 184 | +except for the first which could be shorter than `group_length`, but still must contain at least one character. |
| 185 | +There must be a dash inserted between groups and all lowercase letters should be converted to uppercase. |
| 186 | + |
| 187 | +Example: |
| 188 | + |
| 189 | +Input: `5F3Z-2e-9-w`, 3 |
| 190 | +Output: `5F-3Z2-E9W` |
| 191 | + |
| 192 | +```python |
| 193 | +def license_key_formatter(license_str: str, group_length: int) -> str: |
| 194 | + license_keys = license_str.replace('-', '').upper() |
| 195 | + remainder = len(license_keys) % group_length |
| 196 | + |
| 197 | + start = 0 |
| 198 | + end = remainder if remainder > 0 else group_length |
| 199 | + license_groups = [] |
| 200 | + |
| 201 | + while end <= len(license_keys): |
| 202 | + license_groups.append(license_keys[start:end]) |
| 203 | + start = end |
| 204 | + end += group_length |
| 205 | + |
| 206 | + return '-'.join(license_groups) |
| 207 | +``` |
| 208 | + |
177 | 209 | ## Longest Common Prefix |
178 | 210 |
|
179 | 211 | Write a function to find the longest common prefix string amongst an array of strings. |
|
0 commit comments