Skip to content

Commit ab54de8

Browse files
refactor 555
1 parent d4da273 commit ab54de8

File tree

2 files changed

+30
-13
lines changed

2 files changed

+30
-13
lines changed

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

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,26 @@
11
package com.fishercoder.solutions;
22

33
/**
4+
* 555. Split Concatenated Strings
5+
*
46
* Given a list of strings, you could concatenate these strings together into a loop,
57
* where for each string you could choose to reverse it or not.
8+
*
69
* Among all the possible loops, you need to find the lexicographically biggest string after cutting the loop,
710
* which will make the looped string into a regular one.
11+
*
812
* Specifically, to find the lexicographically biggest string, you need to experience two phases:
9-
10-
Concatenate all the strings into a loop, where you can reverse some strings or not and connect them in the same order as given.
11-
12-
Cut and make one breakpoint in any place of the loop, which will make the looped string into a regular one starting from the character at the cutpoint.
13-
14-
And your job is to find the lexicographically biggest one among all the possible regular strings.
13+
*
14+
* 1. Concatenate all the strings into a loop, where you can reverse some strings or not and connect them in the same order as given.
15+
* 2. Cut and make one breakpoint in any place of the loop, which will make the looped string into a regular one starting from
16+
* the character at the cutpoint.
17+
*
18+
* And your job is to find the lexicographically biggest one among all the possible regular strings.
1519
1620
Example:
1721
Input: "abc", "xyz"
1822
Output: "zyxcba"
23+
1924
Explanation: You can get the looped string "-abcxyz-", "-abczyx-", "-cbaxyz-", "-cbazyx-",
2025
where '-' represents the looped status.
2126
The answer string came from the fourth looped one,
@@ -28,23 +33,27 @@
2833
*/
2934
public class _555 {
3035

31-
//credit: https://discuss.leetcode.com/topic/86477/neat-java-solution and article: https://leetcode.com/articles/split-assembled-strings/#approach-3-optimized-solution-accepted
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+
*/
3240
public String splitLoopedString(String[] strs) {
33-
StringBuilder stringBuilder = new StringBuilder();
41+
StringBuilder sb = new StringBuilder();
3442
for (int i = 0; i < strs.length; i++) {
35-
stringBuilder.setLength(0);
36-
String reverse = stringBuilder.append(strs[i]).reverse().toString();
43+
sb.setLength(0);
44+
String reverse = sb.append(strs[i]).reverse().toString();
3745
if (strs[i].compareTo(reverse) < 0) {
3846
strs[i] = reverse;
3947
}
4048
}
4149
String result = "";
4250
for (int i = 0; i < strs.length; i++) {
43-
stringBuilder.setLength(0);
44-
String reverse = stringBuilder.append(strs[i]).reverse().toString();
51+
sb.setLength(0);
52+
String reverse = sb.append(strs[i]).reverse().toString();
4553
for (String str : new String[]{strs[i], reverse}) {
4654
for (int k = 0; k < str.length(); k++) {
47-
StringBuilder sb = new StringBuilder(str.substring(k));
55+
sb.setLength(0);
56+
sb.append(str.substring(k));
4857
for (int j = i + 1; j < strs.length; j++) {
4958
sb.append(strs[j]);
5059
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,12 @@ public void test1() {
2727
actual = test.splitLoopedString(strs);
2828
assertEquals(expected, actual);
2929
}
30+
31+
@Test
32+
public void test2() {
33+
strs = new String[]{"lc", "evol", "cdy"};
34+
expected = "ylclovecd";
35+
actual = test.splitLoopedString(strs);
36+
assertEquals(expected, actual);
37+
}
3038
}

0 commit comments

Comments
 (0)