Using StringBuilder beat 96%


  • 0

    public class LicenseKeyFormatting {
    public String licenseKeyFormatting(String S, int K) {
    String[] strs = S.split("-");
    if (strs.length==0) return "";
    StringBuilder sb = new StringBuilder();
    for (String str : strs) sb.append(str.toUpperCase());
    int end = sb.length()%K;
    if (end==0) end += K;
    StringBuilder res = new StringBuilder();
    while (end<=sb.length()) {
    res.append(sb.substring(Math.max(0, end - K), end)).append("-");
    end += K;
    }
    return res.substring(0, res.length() - 1);
    }
    }


Log in to reply
 

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