arcbjorn

thoughtbook

Thursday, December 29, 2022

License key formatting

Leetcode 482 problem.

Go

  • Time complexity: O(nlogn)O(n * log_n) - n is a length of a string
  • Auxiliary space: O(1)O(1) - constant amount of space
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
}
Creative Commons Licence