You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<strong>Explanation:</strong> You can get the looped string "-abcxyz-", "-abczyx-", "-cbaxyz-", "-cbazyx-", where '-' represents the looped status.
40
+
<strong>Explanation:</strong> You can get the looped string "-abcxyz-", "-abczyx-", "-cbaxyz-", "-cbazyx-", where '-' represents the looped status.
41
41
The answer string came from the fourth looped one, where you could cut from the middle character 'a' and get "zyxcba".
42
42
</pre>
43
43
@@ -64,7 +64,15 @@ The answer string came from the fourth looped one, where you could cut from the
64
64
65
65
<!-- solution:start -->
66
66
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`.
0 commit comments