482. License Key Formatting LeetCode Solution

In this guide, you will get 482. License Key Formatting LeetCode Solution with the best time and space complexity. The solution to License Key Formatting problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. License Key Formatting solution in C++
  4. License Key Formatting solution in Java
  5. License Key Formatting solution in Python
  6. Additional Resources
482. License Key Formatting LeetCode Solution image

Problem Statement of License Key Formatting

You are given a license key represented as a string s that consists of only alphanumeric characters and dashes. The string is separated into n + 1 groups by n dashes. You are also given an integer k.
We want to reformat the string s such that each group contains exactly k characters, except for the first group, which could be shorter than k but still must contain at least one character. Furthermore, there must be a dash inserted between two groups, and you should convert all lowercase letters to uppercase.
Return the reformatted license key.

Example 1:

Input: s = “5F3Z-2e-9-w”, k = 4
Output: “5F3Z-2E9W”
Explanation: The string s has been split into two parts, each part has 4 characters.
Note that the two extra dashes are not needed and can be removed.

Example 2:

Input: s = “2-5g-3-J”, k = 2
Output: “2-5G-3J”
Explanation: The string s has been split into three parts, each part has 2 characters except the first part as it could be shorter as mentioned above.

Constraints:

1 <= s.length <= 105
s consists of English letters, digits, and dashes '-'.
1 <= k <= 104

See also  467. Unique Substrings in Wraparound String LeetCode Solution

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(n)

482. License Key Formatting LeetCode Solution in C++

class Solution {
 public:
  string licenseKeyFormatting(string s, int k) {
    string ans;
    int length = 0;

    for (int i = s.length() - 1; i >= 0; --i) {
      if (s[i] == '-')
        continue;
      if (length > 0 && length % k == 0)
        ans += "-";
      ans += toupper(s[i]);
      ++length;
    }

    ranges::reverse(ans);
    return ans;
  }
};
/* code provided by PROGIEZ */

482. License Key Formatting LeetCode Solution in Java

class Solution {
  public String licenseKeyFormatting(String s, int k) {
    StringBuilder sb = new StringBuilder();
    int length = 0;

    for (int i = s.length() - 1; i >= 0; --i) {
      if (s.charAt(i) == '-')
        continue;
      if (length > 0 && length % k == 0)
        sb.append('-');
      sb.append(Character.toUpperCase(s.charAt(i)));
      ++length;
    }

    return sb.reverse().toString();
  }
}
// code provided by PROGIEZ

482. License Key Formatting LeetCode Solution in Python

class Solution:
  def licenseKeyFormatting(self, s: str, k: int) -> str:
    ans = []
    length = 0

    for i in reversed(range(len(s))):
      if s[i] == '-':
        continue
      if length > 0 and length % k == 0:
        ans += '-'
      ans += s[i].upper()
      length += 1

    return ''.join(reversed(ans))
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.