Thursday, December 29, 2022
import "strings"
func licenseKeyFormatting(s string, k int) string {
upperCaseS := strings.ToUpper(s)
str := strings.Replace(upperCaseS, "-", "", -1)
lastComboLastCharIdx := len(str) - 1 - k;
// starting from the end
// jumping over `k` characters
// till first character
for i := lastComboLastCharIdx; i >= 0; i = i - k {
// concantination of 2 strings:
// string up untill the begining of the latest combo
// start of the string of latest combo till the end
str = str[0:i + 1] + "-" + str[i + 1:]
}
return str
}