My take in C#


  • 0
    S

    public string LicenseKeyFormatting(string S, int K) {
    string s = S.Replace("-", "");
    int remaining = s.Length;
    List<string> sb = new List<string>();

        while(remaining > 0)
        {
            int take = remaining < K ? remaining : K;
            sb.Add(s.Substring(Math.Max(remaining - K, 0), take).ToUpper());
            remaining -= K;
        }
        sb.Reverse();
        return string.Join("-", sb);
    }

Log in to reply
 

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