Skip to content

Commit dc9bac1

Browse files
refactor 482
1 parent 741b222 commit dc9bac1

File tree

2 files changed

+39
-39
lines changed

2 files changed

+39
-39
lines changed
Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,59 @@
11
package com.fishercoder.solutions;
22

33
/**
4-
* 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.
5-
6-
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.
7-
8-
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.
4+
* 482. License Key Formatting
5+
*
6+
* Now you are given a string S, which represents a software license key which we would like to format.
7+
* The string S is composed of alphanumerical characters and dashes.
8+
* The dashes split the alphanumerical characters within the string into groups.
9+
* (i.e. if there are m dashes, the string is split into m+1 groups).
10+
* The dashes in the given string are possibly misplaced.
11+
* We want each group of characters to be of length K
12+
* (except for possibly the first group, which could be shorter, but still must contain at least one character).
13+
* To satisfy this requirement, we will reinsert dashes. Additionally, all the lower case letters in the string must be converted to upper case.
14+
* So, you are given a non-empty string S, representing a license key to format, and an integer K.
15+
* And you need to return the license key formatted according to the description above.
916
1017
Example 1:
11-
1218
Input: S = "2-4A0r7-4k", K = 4
13-
1419
Output: "24A0-R74K"
15-
1620
Explanation: The string S has been split into two parts, each part has 4 characters.
1721
1822
Example 2:
19-
2023
Input: S = "2-4A0r7-4k", K = 3
21-
2224
Output: "24-A0R-74K"
23-
2425
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.
2526
2627
Note:
27-
2828
The length of string S will not exceed 12,000, and K is a positive integer.
2929
String S consists only of alphanumerical characters (a-z and/or A-Z and/or 0-9) and dashes(-).
3030
String S is non-empty.
31-
3231
*/
3332
public class _482 {
34-
35-
public String licenseKeyFormatting(String S, int K) {
36-
StringBuilder stringBuilder = new StringBuilder();
37-
char[] SChars = S.toCharArray();
38-
for (int i = S.length() - 1, j = 0; i >= 0; ) {
39-
if (j < K) {
40-
if (SChars[i] != '-') {
41-
if (SChars[i] >= 'a' && SChars[i] <= 'z') {
42-
stringBuilder.append(Character.toUpperCase(SChars[i]));
43-
} else {
44-
stringBuilder.append(SChars[i]);
33+
public static class Solution1 {
34+
public String licenseKeyFormatting(String S, int K) {
35+
StringBuilder stringBuilder = new StringBuilder();
36+
char[] SChars = S.toCharArray();
37+
for (int i = S.length() - 1, j = 0; i >= 0; ) {
38+
if (j < K) {
39+
if (SChars[i] != '-') {
40+
if (SChars[i] >= 'a' && SChars[i] <= 'z') {
41+
stringBuilder.append(Character.toUpperCase(SChars[i]));
42+
} else {
43+
stringBuilder.append(SChars[i]);
44+
}
45+
j++;
4546
}
46-
j++;
47+
i--;
48+
} else if (j == K) {
49+
j = 0;
50+
stringBuilder.append('-');
4751
}
48-
i--;
49-
} else if (j == K) {
50-
j = 0;
51-
stringBuilder.append('-');
5252
}
53+
if (stringBuilder.length() > 1 && stringBuilder.substring(stringBuilder.length() - 1).equals("-")) {
54+
return stringBuilder.reverse().substring(1);
55+
}
56+
return stringBuilder.reverse().toString();
5357
}
54-
if (stringBuilder.length() > 1 && stringBuilder.substring(stringBuilder.length() - 1).equals("-")) {
55-
return stringBuilder.reverse().substring(1);
56-
}
57-
return stringBuilder.reverse().toString();
5858
}
5959
}

src/test/java/com/fishercoder/_482Test.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@
88
import static junit.framework.Assert.assertEquals;
99

1010
public class _482Test {
11-
private static _482 test;
11+
private static _482.Solution1 solution1;
1212
private static String expected;
1313
private static String actual;
1414
private static String S;
1515
private static int k;
1616

1717
@BeforeClass
1818
public static void setup() {
19-
test = new _482();
19+
solution1 = new _482.Solution1();
2020
}
2121

2222
@Before
@@ -30,7 +30,7 @@ public void test1() {
3030
S = "2-4A0r7-4k";
3131
k = 4;
3232
expected = "24A0-R74K";
33-
actual = test.licenseKeyFormatting(S, k);
33+
actual = solution1.licenseKeyFormatting(S, k);
3434
assertEquals(expected, actual);
3535
}
3636

@@ -39,7 +39,7 @@ public void test2() {
3939
S = "2-4A0r7-4k";
4040
k = 3;
4141
expected = "24-A0R-74K";
42-
actual = test.licenseKeyFormatting(S, k);
42+
actual = solution1.licenseKeyFormatting(S, k);
4343
assertEquals(expected, actual);
4444
}
4545

@@ -48,7 +48,7 @@ public void test3() {
4848
S = "--a-a-a-a--";
4949
k = 2;
5050
expected = "AA-AA";
51-
actual = test.licenseKeyFormatting(S, k);
51+
actual = solution1.licenseKeyFormatting(S, k);
5252
assertEquals(expected, actual);
5353
}
5454

@@ -57,7 +57,7 @@ public void test4() {
5757
S = "---";
5858
k = 3;
5959
expected = "";
60-
actual = test.licenseKeyFormatting(S, k);
60+
actual = solution1.licenseKeyFormatting(S, k);
6161
assertEquals(expected, actual);
6262
}
6363
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy