|
1 | 1 | package com.fishercoder.solutions; |
2 | 2 |
|
3 | 3 | /** |
| 4 | + * 555. Split Concatenated Strings |
| 5 | + * |
4 | 6 | * Given a list of strings, you could concatenate these strings together into a loop, |
5 | 7 | * where for each string you could choose to reverse it or not. |
| 8 | + * |
6 | 9 | * Among all the possible loops, you need to find the lexicographically biggest string after cutting the loop, |
7 | 10 | * which will make the looped string into a regular one. |
| 11 | + * |
8 | 12 | * 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. |
15 | 19 |
|
16 | 20 | Example: |
17 | 21 | Input: "abc", "xyz" |
18 | 22 | Output: "zyxcba" |
| 23 | +
|
19 | 24 | Explanation: You can get the looped string "-abcxyz-", "-abczyx-", "-cbaxyz-", "-cbazyx-", |
20 | 25 | where '-' represents the looped status. |
21 | 26 | The answer string came from the fourth looped one, |
|
28 | 33 | */ |
29 | 34 | public class _555 { |
30 | 35 |
|
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 | + */ |
32 | 40 | public String splitLoopedString(String[] strs) { |
33 | | - StringBuilder stringBuilder = new StringBuilder(); |
| 41 | + StringBuilder sb = new StringBuilder(); |
34 | 42 | 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(); |
37 | 45 | if (strs[i].compareTo(reverse) < 0) { |
38 | 46 | strs[i] = reverse; |
39 | 47 | } |
40 | 48 | } |
41 | 49 | String result = ""; |
42 | 50 | 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(); |
45 | 53 | for (String str : new String[]{strs[i], reverse}) { |
46 | 54 | 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)); |
48 | 57 | for (int j = i + 1; j < strs.length; j++) { |
49 | 58 | sb.append(strs[j]); |
50 | 59 | } |
|
0 commit comments