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:

  1. 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.

  2. All letters in the license plate number should be capitalized.

  3. The digits in the license plate number should be separated into groups of K characters but no dashes are needed.

  4. The license plate number must be exactly K groups of characters.

  5. 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.

  6. 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:

  1. Convert the incoming string to uppercase.

  2. Remove all the dashes from the string.

  3. Calculate the length of the resulting string after removing the dashes.

  4. Find the length of the first group by taking the modulus of the length with K.

  5. Add the first group to the result string.

  6. For every group of K characters after the first group, add a dash followed by that group of K characters.

  7. 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