Similar Problems
Similar Problems not available
License Key Formatting - Leetcode Solution
Companies:
LeetCode: License Key Formatting Leetcode Solution
Difficulty: Easy
Topics: string
Problem Statement:
Given a string S representing the license plate number and an integer K, return the license plate number formatted according to the following rules:
-
The license plate number is divided into groups of K characters, separated by dashes '-'. The first group may have fewer than K characters, and all subsequent groups should have exactly K characters.
-
All letters in the license plate number should be capitalized.
-
The digits in the license plate number should be separated into groups of K characters but no dashes are needed.
-
The license plate number must be exactly K groups of characters.
-
The formula for the license plate number is "A1-A2-A3-...-Ak" where A1, A2,...,Ak are groups of letters or digits satisfying the rules above.
-
Return the license plate number in any valid format.
Example 1:
Input: S = "5F3Z-2e-9-w", K = 4
Output: "5F3Z-2E9W"
Explanation: The license plate number is "5F3Z-2E9W". The output is formatted as follows:
First group: 5F3Z
Second group: 2E9W
Note that the first group is not exactly K characters long, but can still be included in the output because the second group has exactly K characters.
Example 2:
Input: S = "2-5g-3-J", K = 2
Output: "2-5G-3J"
Explanation: The license plate number is "2-5G-3J". The output is formatted as follows:
First group: 2-5G
Second group: 3J
Note that the first group is not exactly K characters long, but can still be included in the output because the second group has exactly K characters.
Solution:
To solve this problem, we can follow the steps given below:
-
Convert the incoming string to uppercase.
-
Remove all the dashes from the string.
-
Calculate the length of the resulting string after removing the dashes.
-
Find the length of the first group by taking the modulus of the length with K.
-
Add the first group to the result string.
-
For every group of K characters after the first group, add a dash followed by that group of K characters.
-
Return the resulting string.
Code:
The code to implement the above solution in Python is given below:
def licenseKeyFormatting(S: str, K: int) -> str:
# Convert incoming string to uppercase
S = S.upper()
# Remove all dashes from the string
S = S.replace('-', '')
# Calculate length of the resulting string
length = len(S)
# Calculate length of the first group
first_group_length = length % K
# Add first group to result string
result = S[:first_group_length]
# For every group of K characters after the first group, add a dash followed by that group of K characters
for i in range(first_group_length, length, K):
result += '-' + S[i:i+K]
# Return the resulting string
return result
Conclusion:
The License Key Formatting problem can be solved efficiently by following the above-mentioned steps. The solution to the problem is also very easy to understand.
License Key Formatting Solution Code
1