Golang beats 100% at 6ms


  • 3
    R
    import (
    	"fmt"
    	"strings"
    )
    
    func licenseKeyFormatting(S string, K int) string {
            S = strings.Replace(S, "-", "", -1)
            S = strings.ToUpper(S)
            mod := len(S) % K
            
            if mod == 0 {
                mod += K
            }
            for mod < len(S) {
                S = S[:mod] + "-" + S[mod:]
                mod += K + 1
            }
            return S
    }
    
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.