## Algorithm

Problem Name: 482. 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.

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`

## Code Examples

### #1 Code Example with C Programming

```Code - C Programming```

``````
char* licenseKeyFormatting(char* S, int K) {
char *p;
int l, n, m;
int i;

p = S;
l = 0;
while (*p) {
if (*p != '-') l ++;  // get length all valid characters
p ++;
}

if (l == 0) return "";

n = l / K;          // total number of groups having full of K characters
m = K - (l % K);    // start number of first group

p = malloc(l + n + 1);
//assert(p);

while (c = *(S ++)) {
if (c == '-') continue;
*p = (c >= 'a' && c <= 'z') ? c - 'a' + 'A': c;
p ++;
m = (m + 1) % K;
if (m == 0) {
*p = '-';
p ++;
}
}
if (*(p - 1) == '-') p --;
*p = 0;

}
``````
Copy The Code &

Input

cmd
s = "5F3Z-2e-9-w", k = 4

Output

cmd
"5F3Z-2E9W"

### #2 Code Example with C++ Programming

```Code - C++ Programming```

``````
class Solution {
public:
string licenseKeyFormatting(string S, int K) {
stringstream ss(S);
string s = "", tmp = "", res = "";
while(getline(ss, tmp, '-')) s += tmp;
for(auto& c: s) c = toupper(c);
int i = 0, step = (s.size() % K == 0) ? K : s.size() % K;
while(i < s.size()){
res += s.substr(i, step) + '-';
i += step;
step = K;
}
res.pop_back();
return res;
}
};
``````
Copy The Code &

Input

cmd
s = "5F3Z-2e-9-w", k = 4

Output

cmd
"5F3Z-2E9W"

### #3 Code Example with Java Programming

```Code - Java Programming```

``````
class Solution {
public String licenseKeyFormatting(String S, int K) {
StringBuilder sb = new StringBuilder();
int idx = S.length() - 1;
int count = 0;

while (idx >= 0) {
if (S.charAt(idx) != '-') {
if (Character.isLetter(S.charAt(idx))) {
sb.append(Character.toUpperCase(S.charAt(idx)));
}
else {
sb.append(S.charAt(idx));
}
count++;
}

idx--;

if (count == K && idx >= 0) {
sb.append('-');
count = 0;
}
}

if (sb.length() == 0) {
return "";
}

String ans = sb.reverse().toString();
return ans.charAt(0) == '-' ? ans.substring(1) : ans;
}
}
``````
Copy The Code &

Input

cmd
s = "2-5g-3-J", k = 2

Output

cmd
"2-5G-3J"

### #4 Code Example with Javascript Programming

```Code - Javascript Programming```

``````
const licenseKeyFormatting = function(S, K) {
if (S == null || S === "") return "";
const newStr = S.replace(/-/g, "").toUpperCase();
const arr = newStr.split("");
for (let i = arr.length - 1 - K; i >= 0; i -= K) {
arr[i] = arr[i] + "-";
}
return arr.join("");
};
``````
Copy The Code &

Input

cmd
s = "2-5g-3-J", k = 2

Output

cmd
"2-5G-3J"

### #5 Code Example with Python Programming

```Code - Python Programming```

``````
class Solution:
S = S.replace("-", "").upper()[::-1]
return '-'.join([S[i:i+K] for i in range(0, len(S), K)])[::-1]
``````
Copy The Code &

Input

cmd
s = "5F3Z-2e-9-w", k = 4

Output

cmd
"5F3Z-2E9W"

### #6 Code Example with C# Programming

```Code - C# Programming```

``````
using System.Text;

namespace LeetCode
{
{
public string LicenseKeyFormatting(string S, int K)
{
S = S.ToUpper().Replace("-", "");
var sb = new StringBuilder();
var groupSize = S.Length % K;
if (groupSize == 0) groupSize = K;

for (int i = 0; i < S.Length; i++)
{
if (groupSize == 0)
{
sb.Append("-");
groupSize = K;
}

sb.Append(S[i]);
groupSize--;
}

return sb.ToString();
}
}
}
``````
Copy The Code &

Input

cmd
s = "5F3Z-2e-9-w", k = 4

Output

cmd
"5F3Z-2E9W"