Leet Code OJ 482. License Key Formatting [Difficulty: Medium]

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Lnho2015/article/details/54846660

题目

Now you are given a string S, which represents a software license key which we would like to format. The string S is composed of alphanumerical characters and dashes. The dashes split the alphanumerical characters within the string into groups. (i.e. if there are M dashes, the string is split into M+1 groups). The dashes in the given string are possibly misplaced.

We want each group of characters to be of length K (except for possibly the first group, which could be shorter, but still must contain at least one character). To satisfy this requirement, we will reinsert dashes. Additionally, all the lower case letters in the string must be converted to upper case.

So, you are given a non-empty string S, representing a license key to format, and an integer K. And you need to return the license key formatted according to the description above.

Example 1:

Input: S = "2-4A0r7-4k", K = 4

Output: "24A0-R74K"

Explanation: The string S has been split into two parts, each part has 4 characters.

Example 2:

Input: S = "2-4A0r7-4k", K = 3

Output: "24-A0R-74K"

Explanation: The string S has been split into three parts, each part has 3 characters except the first part as it could be shorter as said above.

Note:

  1. The length of string S will not exceed 12,000, and K is a positive integer.
  2. String S consists only of alphanumerical characters (a-z and/or A-Z and/or 0-9) and dashes(-).
  3. String S is non-empty.

翻译

给定一个非空字符串S,代表一个软件授权秘钥,我们需要格式化这个秘钥。这个字符串由字母、数字(a-z and/or A-Z and/or 0-9)和中划线(-)组成。中划线把字符串S分为几个组(例如有M个中划线,那会被分为M+1个组)。现在,中划线放置的位置可能被放错了。
我们希望每个组的字符长度为整数K(除了第一个组,允许少于K,但是至少要有一个字符)。为了满足这个需求,我们需要重新放置中划线。另外,还需要把所有小写字母转换为大写字母。
提示:
1. S的长度不会超过12000,K是一个正整数。
2. S只包含字母、数字和中划线。
3. S非空。

分析

由于给定的字符串S还无法判断直接有效字符(除中划线外)的个数,故先做一次遍历。笔者在这次遍历中,将所有有效字符全部左移了,并转化了大小写。
得到有效字符的个数后,就可以算出最终字符的长度newLen了,进而创建这个新字符数组。遍历这个新数组进行填充即可。

Java版代码(时间复杂度O(n),空间复杂度O(n)):

public class Solution {
    public String licenseKeyFormatting(String S, int K) {
        int len = 0;
        int up = 'a' - 'A';
        char[] charArr = S.toCharArray();
        for (int i = 0; i <= charArr.length - 1; i++) {
            char ch = charArr[i];
            if (ch == '-') {
                continue;
            }
            if (ch >= 'a' && ch <= 'z') {
                charArr[i] -= up;
            }
            charArr[len] = charArr[i];
            len++;
        }
        if(len==0) return "";
        int newLen = len % K == 0 ? len + len / K - 1 : len + len / K;
        char[] newArr = new char[newLen];
        int j = len;
        for (int i = newLen - 1; i >= 0; i--) {
            if (i != newLen - 1 && (newLen-i) % (K+1) == 0) {
                newArr[i] = '-';
            } else {
                newArr[i] = charArr[--j];
            }
        }
        return String.valueOf(newArr);
    }
}

猜你喜欢

转载自blog.csdn.net/Lnho2015/article/details/54846660