問(wèn)題:
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:
- 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.
大意:
現(xiàn)在給你一個(gè)字符串S碉哑,代表我們想要組成的一個(gè)軟件的序列號(hào)踏揣。字符串S由數(shù)字和字母以及破折號(hào)組成狭瞎。破折號(hào)將數(shù)字和字母分割成一組組。(比如程梦,如果有M個(gè)破折號(hào),則字符串被分為M+1組)橘荠。字符串中的破折號(hào)可能放錯(cuò)了位置屿附。
我們想要每組字符的長(zhǎng)度為K(除了第一組可能短一些,但必須至少包含一個(gè)字符)哥童。要滿足這些要求挺份,我們會(huì)重新插入破折號(hào)。此外贮懈,所有的小寫(xiě)字母必須轉(zhuǎn)換成大寫(xiě)字母匀泊。
所以,給你一個(gè)非空字符串S朵你,代表要組成的序列號(hào)燃乍,以及一個(gè)整數(shù)K墨技。你需要根據(jù)上面的描述返回正確的序列號(hào)。
例1:輸入: S = "2-4A0r7-4k", K = 4
輸出:"24A0-R74K"
解釋:字符串S被分為兩組,每組有4個(gè)字符淀衣。例2:
輸入:S = "2-4A0r7-4k", K = 3
輸出:"24-A0R-74K"
解釋:字符串S被分為三部分,每部分有三個(gè)字符屁置,除了第一部分如上所說(shuō)可以短一點(diǎn)疆虚。注意:
- 字符串S的長(zhǎng)度不會(huì)超過(guò)12000,K是個(gè)正數(shù)水孩。
- 字符串S只由數(shù)字及字母(a-z 和/或 A-Z 和/或 0-9)以及破折號(hào)(-)組成镰矿。
- 字符串S非空。
思路:
題目說(shuō)了一長(zhǎng)串荷愕,其實(shí)總結(jié)起來(lái)就是:
給一個(gè)字符串和正整數(shù)衡怀,將字符串用破折號(hào)分成多個(gè)長(zhǎng)度為K的組(第一組可以小于K)棍矛,所有字母必須為大寫(xiě)。
其實(shí)還是很容易的抛杨,因?yàn)榈谝唤M不一定長(zhǎng)度為K够委,所以我們從后往前來(lái)重組,遇到小寫(xiě)字母就換成大寫(xiě)字母怖现,結(jié)果中每放完K個(gè)字符就加一個(gè)破折號(hào)茁帽,遍歷字符串時(shí)遇到破折號(hào)直接跳過(guò),為了速度我們使用StringBuffer來(lái)處理結(jié)果字符串屈嗤,處理完后再將結(jié)果翻轉(zhuǎn)潘拨,才是正確的順序。注意最后可能會(huì)在結(jié)果的開(kāi)頭出現(xiàn)一個(gè)破折號(hào)饶号,也就是未翻轉(zhuǎn)前的最后铁追,這時(shí)候要去除掉。
代碼(Java):
public class Solution {
public String licenseKeyFormatting(String S, int K) {
char[] sArr = S.toCharArray();
String result = "";
StringBuffer sBuffer = new StringBuffer(result);
int length = 0;
for (int i = sArr.length-1; i >= 0; i--) {
if (sArr[i] - '-' == 0) continue;
if (sArr[i] - 'a' >= 0) sArr[i] = (char)(sArr[i] - 32);
sBuffer.append(sArr[i]);
length ++;
if (length == K) {
sBuffer.append('-');
length = 0;
}
}
if (sBuffer.length() > 0 && sBuffer.charAt(sBuffer.length()-1) - '-' == 0) sBuffer.delete(sBuffer.length()-1, sBuffer.length());
sBuffer.reverse();
result = sBuffer.toString();
return result;
}
}
他山之石:
public String licenseKeyFormatting(String s, int k) {
StringBuilder sb = new StringBuilder();
for (int i = s.length() - 1; i >= 0; i--)
if (s.charAt(i) != '-')
sb.append(sb.length() % (k + 1) == k ? '-' : "").append(s.charAt(i));
return sb.reverse().toString().toUpperCase();
}
上面的代碼時(shí)對(duì)前面的思路的極致簡(jiǎn)潔茫船。
合集:https://github.com/Cloudox/LeetCode-Record