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
Copy file name to clipboardexpand all lines: solution/1800-1899/1859.Sorting the Sentence/README_EN.md
+50-34
Original file line number
Diff line number
Diff line change
@@ -44,18 +44,35 @@
44
44
45
45
## Solutions
46
46
47
+
**Solution 1: String Splitting**
48
+
49
+
First, we split the string $s$ by spaces to get the string array $words$. Then, we create a string array $ans$ of length $|words|$ to store the answer.
50
+
51
+
Next, we iterate over each string $w$ in the string array $words$, find the position $i$ represented by the last character of $w$, then take the first $|w|-1$ characters of $w$ as the new string $w'$, and place $w'$ in the $i$th position of the array $ans$.
52
+
53
+
Finally, we join the array $ans$ into a string by spaces, which is the answer.
54
+
55
+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Where $n$ is the length of the string $s$.
0 commit comments