License Key Formatting 2021-10-04 12:13

Problem Description

public String licenseKeyFormatting(String s, int k) {
    s = s.replaceAll("-", "");
    int first = s.length() % k;
    int group = s.length() / k;
    StringBuilder sb = new StringBuilder(s.toUpperCase());
    int addIndex=0;
    for (int i = 0; i < group; i++) {
        int offset = first + k * i+addIndex;
        if (offset > 0) {
            sb.insert(offset, '-');
            addIndex++;
        }
    }
    return sb.toString();
}
Runtime Memory
29 ms 39.2 MB

henryxi leetcode list

EOF