Skip to content
Programming101
Programming101

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programming101

Learn everything about programming

Leetcode License Key Formatting problem solution

YASH PAL, 31 July 2024

In this Leetcode License Key Formatting problem solution 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.

Leetcode License Key Formatting problem solution

Problem solution in Python.

class Solution(object):
    def licenseKeyFormatting(self, S, K):
        s = S.replace('-', '').upper()
        ngroups, rem = len(s) / K , len(s) % K
        S = s[0:rem]
        for i in range(ngroups):
            if i > 0 or rem > 0: S += '-'
            S += s[rem+i*K:rem+(i+1)*K]
        return S

Problem solution in Java.

public String licenseKeyFormatting(String S, int K) {
        StringBuilder sb = new StringBuilder(); 
        S = S.replace("-", ""); 
        S = S.toUpperCase();
        if (S.length() == 0) {
            return S; 
        }
        int i = S.length() - 1; 
        while (i >= 0) {
            int count = 0; 
            while (count < K && i >= 0) {
                count++; 
                sb.append(S.charAt(i--)); 
            }
            sb.append("-"); 
        }
        
        sb.deleteCharAt(sb.length() - 1); 
        return sb.reverse().toString(); 
    }

Problem solution in C++.

string licenseKeyFormatting(string S, int K) {
        if (S.empty()) return S;
 
        std::string key;
        key.reserve(2*S.length());
        int len = 0;
        for_each(S.rbegin(), S.rend(), [&](char c)
        {
            if (c != '-')
            {
                if(len == K)
                {
                    key += "-";
                    len = 0;
                }
                ++len;
                key += std::toupper(c);
            }
        });
        std::reverse(key.begin(), key.end());
        return key;
        
    }

coding problems

Post navigation

Previous post
Next post
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
  • Hackerrank Day 6 Lets Review 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
©2025 Programming101 | WordPress Theme by SuperbThemes