Skip to content

Commit 179e054

Browse files
refactor 555
1 parent 3977b5a commit 179e054

File tree

2 files changed

+37
-35
lines changed

2 files changed

+37
-35
lines changed

src/main/java/com/fishercoder/solutions/_555.java

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -33,41 +33,43 @@
3333
*/
3434
public class _555 {
3535

36-
/**
37-
* credit: https://discuss.leetcode.com/topic/86477/neat-java-solution
38-
* and article: https://leetcode.com/articles/split-assembled-strings/#approach-3-optimized-solution-accepted
39-
*/
40-
public String splitLoopedString(String[] strs) {
41-
StringBuilder sb = new StringBuilder();
42-
for (int i = 0; i < strs.length; i++) {
43-
sb.setLength(0);
44-
String reverse = sb.append(strs[i]).reverse().toString();
45-
if (strs[i].compareTo(reverse) < 0) {
46-
strs[i] = reverse;
36+
public static class Solution1 {
37+
38+
/**
39+
* credit: https://discuss.leetcode.com/topic/86477/neat-java-solution
40+
* and article: https://leetcode.com/articles/split-assembled-strings/#approach-3-optimized-solution-accepted
41+
*/
42+
public String splitLoopedString(String[] strs) {
43+
StringBuilder sb = new StringBuilder();
44+
for (int i = 0; i < strs.length; i++) {
45+
sb.setLength(0);
46+
String reverse = sb.append(strs[i]).reverse().toString();
47+
if (strs[i].compareTo(reverse) < 0) {
48+
strs[i] = reverse;
49+
}
4750
}
48-
}
49-
String result = "";
50-
for (int i = 0; i < strs.length; i++) {
51-
sb.setLength(0);
52-
String reverse = sb.append(strs[i]).reverse().toString();
53-
for (String str : new String[]{strs[i], reverse}) {
54-
for (int k = 0; k < str.length(); k++) {
55-
sb.setLength(0);
56-
sb.append(str.substring(k));
57-
for (int j = i + 1; j < strs.length; j++) {
58-
sb.append(strs[j]);
59-
}
60-
for (int j = 0; j < i; j++) {
61-
sb.append(strs[j]);
62-
}
63-
sb.append(str.substring(0, k));
64-
if (sb.toString().compareTo(result) > 0) {
65-
result = sb.toString();
51+
String result = "";
52+
for (int i = 0; i < strs.length; i++) {
53+
sb.setLength(0);
54+
String reverse = sb.append(strs[i]).reverse().toString();
55+
for (String str : new String[]{strs[i], reverse}) {
56+
for (int k = 0; k < str.length(); k++) {
57+
sb.setLength(0);
58+
sb.append(str.substring(k));
59+
for (int j = i + 1; j < strs.length; j++) {
60+
sb.append(strs[j]);
61+
}
62+
for (int j = 0; j < i; j++) {
63+
sb.append(strs[j]);
64+
}
65+
sb.append(str.substring(0, k));
66+
if (sb.toString().compareTo(result) > 0) {
67+
result = sb.toString();
68+
}
6669
}
6770
}
6871
}
72+
return result;
6973
}
70-
return result;
7174
}
72-
7375
}

src/test/java/com/fishercoder/_555Test.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,29 @@
1010
* Created by fishercoder on 4/29/17.
1111
*/
1212
public class _555Test {
13-
private static _555 test;
13+
private static _555.Solution1 solution1;
1414
private static String expected;
1515
private static String actual;
1616
private static String[] strs;
1717

1818
@BeforeClass
1919
public static void setup() {
20-
test = new _555();
20+
solution1 = new _555.Solution1();
2121
}
2222

2323
@Test
2424
public void test1() {
2525
strs = new String[]{"abc", "xyz"};
2626
expected = "zyxcba";
27-
actual = test.splitLoopedString(strs);
27+
actual = solution1.splitLoopedString(strs);
2828
assertEquals(expected, actual);
2929
}
3030

3131
@Test
3232
public void test2() {
3333
strs = new String[]{"lc", "evol", "cdy"};
3434
expected = "ylclovecd";
35-
actual = test.splitLoopedString(strs);
35+
actual = solution1.splitLoopedString(strs);
3636
assertEquals(expected, actual);
3737
}
3838
}

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