Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitab54de8

Browse files
refactor 555
1 parentd4da273 commitab54de8

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
packagecom.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
publicclass_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
publicStringsplitLoopedString(String[]strs) {
33-
StringBuilderstringBuilder =newStringBuilder();
41+
StringBuildersb =newStringBuilder();
3442
for (inti =0;i <strs.length;i++) {
35-
stringBuilder.setLength(0);
36-
Stringreverse =stringBuilder.append(strs[i]).reverse().toString();
43+
sb.setLength(0);
44+
Stringreverse =sb.append(strs[i]).reverse().toString();
3745
if (strs[i].compareTo(reverse) <0) {
3846
strs[i] =reverse;
3947
}
4048
}
4149
Stringresult ="";
4250
for (inti =0;i <strs.length;i++) {
43-
stringBuilder.setLength(0);
44-
Stringreverse =stringBuilder.append(strs[i]).reverse().toString();
51+
sb.setLength(0);
52+
Stringreverse =sb.append(strs[i]).reverse().toString();
4553
for (Stringstr :newString[]{strs[i],reverse}) {
4654
for (intk =0;k <str.length();k++) {
47-
StringBuildersb =newStringBuilder(str.substring(k));
55+
sb.setLength(0);
56+
sb.append(str.substring(k));
4857
for (intj =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+
publicvoidtest2() {
33+
strs =newString[]{"lc","evol","cdy"};
34+
expected ="ylclovecd";
35+
actual =test.splitLoopedString(strs);
36+
assertEquals(expected,actual);
37+
}
3038
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp