Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

Leetcode License Key Formatting problem solution

YASH PAL, 31 July 202422 January 2026

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

Leetcode License Key Formatting 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

License Key Formatting 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 solutions Leetcode Problems Solutions Leetcode

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes