Skip to content

Commit 2e79b90

Browse files
authored
feat: update solutions to lc problem: No.0555 (doocs#3664)
1 parent 9f3a109 commit 2e79b90

File tree

1 file changed

+10
-2
lines changed
  • solution/0500-0599/0555.Split Concatenated Strings

1 file changed

+10
-2
lines changed

solution/0500-0599/0555.Split Concatenated Strings/README_EN.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ tags:
3737
<pre>
3838
<strong>Input:</strong> strs = [&quot;abc&quot;,&quot;xyz&quot;]
3939
<strong>Output:</strong> &quot;zyxcba&quot;
40-
<strong>Explanation:</strong> You can get the looped string &quot;-abcxyz-&quot;, &quot;-abczyx-&quot;, &quot;-cbaxyz-&quot;, &quot;-cbazyx-&quot;, where &#39;-&#39; represents the looped status.
40+
<strong>Explanation:</strong> You can get the looped string &quot;-abcxyz-&quot;, &quot;-abczyx-&quot;, &quot;-cbaxyz-&quot;, &quot;-cbazyx-&quot;, where &#39;-&#39; represents the looped status.
4141
The answer string came from the fourth looped one, where you could cut from the middle character &#39;a&#39; and get &quot;zyxcba&quot;.
4242
</pre>
4343

@@ -64,7 +64,15 @@ The answer string came from the fourth looped one, where you could cut from the
6464

6565
<!-- solution:start -->
6666

67-
### Solution 1
67+
### Solution 1: Greedy
68+
69+
We first traverse the string array `strs`. For each string $s$, if the reversed string $t$ is greater than $s$, we replace $s$ with $t$.
70+
71+
Then we enumerate each position $i$ in the string array `strs` as a split point, dividing the string array `strs` into two parts: $strs[i + 1:]$ and $strs[:i]$. We then concatenate these two parts to get a new string $t$. Next, we enumerate each position $j$ in the current string $strs[i]$. The suffix part is $a = strs[i][j:]$, and the prefix part is $b = strs[i][:j]$. We can concatenate $a$, $t$, and $b$ to get a new string $cur$. If $cur$ is greater than the current answer, we update the answer. This considers the case where $strs[i]$ is reversed. We also need to consider the case where $strs[i]$ is not reversed, i.e., concatenate $a$, $t$, and $b$ in reverse order to get a new string $cur$. If $cur$ is greater than the current answer, we update the answer.
72+
73+
Finally, we return the answer.
74+
75+
The time complexity is $O(n^2)$, and the space complexity is $O(n)$. Here, $n$ is the length of the string array `strs`.
6876

6977
<!-- tabs:start -->
7078

0 commit comments

Comments
 (0)