题目
You are given a license key represented as a string S which consists only alphanumeric character and dashes. The string is separated into N+1 groups by N dashes.
Given a number K, we would want to reformat the strings such that each group contains exactly K characters, except for the first group which could be shorter than K, but still must contain at least one character. Furthermore, there must be a dash inserted between two groups and all lowercase letters should be converted to uppercase.
Given a non-empty string S and a number K, format the string according to the rules described above.
Example 1:
Input: S = "5F3Z-2e-9-w", K = 4
Output: "5F3Z-2E9W"
Explanation:
The string S has been split into two parts, each part has 4 characters.
Note that the two extra dashes are not needed and can be removed.
Example 2:
Input: S = "2-5g-3-J", K = 2
Output: "2-5G-3J"
Explanation:
The string S has been split into three parts, each part has 2 characters except the first part as it could be shorter as mentioned above.
Note:
The length of string S will not exceed 12,000, and K is a positive integer.
String S consists only of alphanumerical characters (a-z and/or A-Z and/or 0-9) and dashes(-).
String S is non-empty.
解析
题干是很好理解的,即为将所给的字符串S,按照K进行分组,分组之间用“-”进行连接。第一个分组数量只要不超过K就保持不变,超过了也进行拆分。
此题的解题思路为:
- 求得所有字母的长度len,第一个分组的长度,添加连接符后的长度;
- 对给定字符串和结果字符串遍历,用三个下标分别遍历给定字符串(j)、结果字符串(i)和连接符的位置(dashIndex)。
难点在于结果字符串连接符的个数的求解,通过和给一个分组“凑单”的方式,便可以求到。
另外在遍历过程中,要注意给定字符串S[j]为'-'时,跳过(++j),否则进行赋值,并且累加dashIndex。当dashIndex==K+1时,置dashIndex=1。
代码
char toUpperChar(char ch);
int keyLength(char* s);
char * licenseKeyFormatting(char * S, int K){
int totalLen = keyLength(S);
int firstGroupLen = totalLen % K;
// indicate characters length can be divided by K with no remainder
if (firstGroupLen == 0)
firstGroupLen = K;
// add dashes and get length
// add (K - 1) length group to gather the first group
// if the first group can divided by K without remainder, so add this group
// is meaningless, else add the group in order to add one dashes in the result
totalLen += (totalLen + (K - 1)) / K - 1;
if (totalLen < 0)
totalLen = 0;
char* formatKey = (char*)malloc((totalLen + 1) * sizeof(char));
formatKey[totalLen] = '\0';
// i is the loop index of the formatKey array
// j is the loop index of K
// dashIndex is to find dash position, initialize to K + 1 - firstGroupLen
// so as to make the D-value for the first group. It will be set to 1 after find
// the first position of dash.
int i = 0, j = 0, dashIndex = K + 1 - firstGroupLen;
while (i < totalLen) {
if (dashIndex == K + 1) {
formatKey[i++] = '-';
dashIndex = 1;
} else {
// if S[j] is equal to '-', then jump to next,
// else upper S[j] and set to formatKey[i]
if (S[j] != '-') {
formatKey[i++] = toUpperChar(S[j]);
++dashIndex;
}
++j;
}
}
return formatKey;
}
int keyLength(char* s) {
int len = 0;
for (int i = 0; s[i] != '\0'; ++i) {
if (s[i] != '-')
++len;
}
return len;
}
char toUpperChar(char ch) {
if (ch >= 'a' && ch <= 'z')
ch -= ('a' - 'A');
return ch;
}
在以上代码中,结果字符串中连接符个数为(totalLen + (K - 1)) / K - 1,即通过添加一个分组来对第一个分组进行凑单。如果第一个分组可以整除K,则添加(K-1)则无意义,如果不能整除K,则添加此分组是为了与其拼凑,最后再将多加的1除去,因为可能加多了。
另外,dashIndex的初始化是K+1-firstGroupLen,是为了第一个分组的长度,即找到第一个连接符的位置,然后将dashIndex置1,由于判等的是K+1,因为下标从1开始。
这样便解决了。
此题比较考察逻辑思维,建议读者在阅读完之后用例子中的字符串进行一下推导,这样可以更好的理解。