Welcome to Subscribe On Youtube
Formatted question description: https://leetcode.ca/all/482.html
482. License Key Formatting
Level
Easy
Description
You are given a license key represented as a string S which consists only alphanumeric character and dashes. The string is separated into N+1 groups by N dashes.
Given a number K, we would want to reformat the strings 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 all lowercase letters should be converted to uppercase.
Given a nonempty string S and a number K, format the string according to the rules described above.
Example 1:
Input: S = “5F3Z2e9w”, K = 4
Output: “5F3Z2E9W”
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 = “25g3J”, K = 2
Output: “25G3J”
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.
Note:
 The length of string S will not exceed 12,000, and K is a positive integer.
 String S consists only of alphanumerical characters (az and/or AZ and/or 09) and dashes().
 String S is nonempty.
Solution
First turn all the letters in S
to upper case and remove all dashes from S
. Then loop over S
from right to left and add a dash before every K
characters. Finally, return the new string.

class Solution { public String licenseKeyFormatting(String S, int K) { S = S.toUpperCase(); S = S.replaceAll("", ""); int length = S.length(); StringBuffer sb = new StringBuffer(S); int count = 0; for (int i = length  1; i > 0; i) { count++; if (count == K) { sb.insert(i, ''); count = 0; } } return sb.toString(); } }

class Solution: def licenseKeyFormatting(self, s: str, k: int) > str: s = s.replace('', '').upper() res = [] cnt = (len(s) % k) or k t = 0 for i, c in enumerate(s): res.append(c) t += 1 if t == cnt: t = 0 cnt = k if i != len(s)  1: res.append('') return ''.join(res) ############ class Solution(object): def licenseKeyFormatting(self, S, K): """ :type S: str :type K: int :rtype: str """ s = S.split("") s = "".join(s) n = len(s) start = n % K res = [] if start != 0: res.append(s[:start].upper()) for k in range(0, (len(s)  start) / K): res.append(s[start:start + K].upper()) start += K return "".join(res)

class Solution { public: string licenseKeyFormatting(string s, int k) { string ss = ""; for (char c : s) { if (c == '') continue; if ('a' <= c && c <= 'z') c += 'A'  'a'; ss += c; } int cnt = ss.size() % k; if (cnt == 0) cnt = k; int t = 0; string res = ""; for (int i = 0; i < ss.size(); ++i) { res += ss[i]; ++t; if (t == cnt) { t = 0; cnt = k; if (i != ss.size()  1) res += ''; } } return res; } };

func licenseKeyFormatting(s string, k int) string { s = strings.ReplaceAll(s, "", "") cnt := len(s) % k if cnt == 0 { cnt = k } t := 0 res := []byte{} for i, c := range s { res = append(res, byte(unicode.ToUpper(c))) t++ if t == cnt { t = 0 cnt = k if i != len(s)1 { res = append(res, byte('')) } } } return string(res) }