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 non-empty string S and a number K, format the string according to the rules described above.

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.



Note:

The length of string S will not exceed 12,000, and K is a positive integer.
String S consists only of alphanumerical characters (a-z and/or A-Z and/or 0-9) and dashes(-).
String S is non-empty.

Difficulty: EASY
Tags: String algorithms

Starter code:
class Solution:
    def licenseKeyFormatting(self, S: str, K: int) -> str:
        

Solutions:
class Solution:

	def licenseKeyFormatting(self, S, K):
		S = S.replace('-', '')[::-1].upper()
		return '-'.join([S[i:i + K] for i in range(0, len(S), K)])[::-1]


class Solution:

	def licenseKeyFormatting(self, S, K):
		S = S.upper().replace('-', '')
		size = len(S)
		s1 = K if size % K == 0 else size % K
		res = S[:s1]
		while s1 < size:
			res += '-' + S[s1:s1 + K]
			s1 += K
		return res


class Solution:

	def licenseKeyFormatting(self, S, K):
		S = S.upper().replace('-', '')
		i = 0
		res = []
		start = len(S) % K
		if start:
			res.append(S[:start])
			i = start
		while i < len(S):
			res.append(S[i:i + K])
			i += K
		return '-'.join(res)


class Solution:

	def licenseKeyFormatting(self, S, K):
		s = list(''.join(S.split('-')))
		tmp = [''.join(s[:len(s) % K])] + [''.join(s[i:i + K]) for i in range(len(s) % K, len(s), K)]
		return '-'.join(filter(None, tmp)).upper()


class Solution:

	def licenseKeyFormatting(self, S, K):
		char_list = self.getChars(S)
		num_groups = len(char_list) // K
		group_1_cnt = len(char_list) % K
		output = ''
		if group_1_cnt > 0:
			output += char_list[0:group_1_cnt]
			if num_groups > 0:
				output += '-'
		for i in range(1, num_groups + 1):
			start = i * K + group_1_cnt - K
			finish = start + K
			output += char_list[start:finish]
			if i != num_groups:
				output += '-'
		return output

	def getChars(self, S):
		char_list = ''
		for char in S:
			if char != '-':
				char_list += char.upper()
		return char_list


class Solution:

	def licenseKeyFormatting(self, S, K):
		key = list(S.replace('-', '').upper())
		offset = len(key) % K
		if offset == 0:
			offset = K
		new_key = []
		i = 0
		while i < len(key):
			if i == 0:
				new_key.extend(key[:offset])
				i = offset
			else:
				new_key.append('-')
				new_key.extend(key[i:i + K])
				i = i + K
		return ''.join(new_key)


class Solution:

	def licenseKeyFormatting(self, S, K):
		S = S.upper().replace('-', '')
		first = len(S) % K
		if first == 0:
			first = K
		blocks = [S[:first]]
		pos = first
		for k in range(len(S) // K):
			block = S[pos:pos + K]
			if len(block) > 0:
				blocks.append(block)
			pos += K
		return '-'.join(blocks)


class Solution:

	def licenseKeyFormatting(self, S, K):
		S = S.upper().replace('-', '')
		index = K if len(S) % K == 0 and len(S) > 0 else len(S) % K
		result = S[:index]
		while index < len(S):
			result += '-' + S[index:index + K]
			index += K
		return result


class Solution:

	def licenseKeyFormatting(self, S, K):
		S = S.replace('-', '')
		l = len(S)
		res = l % K
		if res == 0:
			return '-'.join([S[i * K:(i + 1) * K].upper() for i in range(l // K)])
		else:
			lst = [S[0:res].upper()]
			return '-'.join(lst + [S[i * K + res:(i + 1) * K + res].upper() for i in range(l // K)])


class Solution:

	def licenseKeyFormatting(self, S, K):
		S = ''.join(S.split('-'))
		S = S.upper()
		num_group = len(S) // K
		start_idx = len(S) % K
		ans = '' if start_idx == 0 else S[:start_idx]
		for i in range(num_group):
			end_idx = start_idx + K
			if start_idx > 0:
				ans += '-'
			ans += S[start_idx:end_idx]
			start_idx = end_idx
		return ans


class Solution:

	def licenseKeyFormatting(self, S, K):
		s = list(S.upper())
		s = [c for c in s if c != '-']
		groups = []
		while s:
			group = s[-K:]
			groups.append(''.join(group))
			s[-K:] = []
		groups.reverse()
		return '-'.join(groups)


class Solution:

	def licenseKeyFormatting(self, S, K):
		if len(S) <= 1:
			return S.upper().strip('-')
		S = S.upper()
		n_dash = S.count('-')
		n = len(S) - n_dash
		form_S = ''
		if n % K == 0:
			begin_at = K - 1
		else:
			left = n % K
			begin_at = left - 1
		steps = 0
		abs_ind = 0
		norm_ind = 0
		while abs_ind <= begin_at and norm_ind < len(S):
			if S[norm_ind] == '-':
				norm_ind += 1
				continue
			form_S += S[norm_ind]
			abs_ind += 1
			norm_ind += 1
		form_S += '-'
		for char_ind in range(norm_ind, len(S)):
			if S[char_ind] == '-':
				continue
			elif steps == K - 1 and abs_ind > begin_at:
				form_S += S[char_ind] + '-'
				steps = 0
			else:
				form_S += S[char_ind]
				steps += 1
			abs_ind += 1
		return form_S.strip('-')