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/1500-1599/1585.Check If String Is Transformable With Substring Sort Operations/README_EN.md
+11-1Lines changed: 11 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -79,7 +79,17 @@ tags:
79
79
80
80
<!-- solution:start -->
81
81
82
-
### Solution 1
82
+
### Solution 1: Bubble Sort
83
+
84
+
The problem is essentially equivalent to determining whether any substring of length 2 in string $s$ can be swapped using bubble sort to obtain $t$.
85
+
86
+
Therefore, we use an array $pos$ of length 10 to record the indices of each digit in string $s,ドル where $pos[i]$ represents the list of indices where digit $i$ appears, sorted in ascending order.
87
+
88
+
Next, we iterate through string $t$. For each character $t[i]$ in $t,ドル we convert it to the digit $x$. We check if $pos[x]$ is empty. If it is, it means that the digit in $t$ does not exist in $s,ドル so we return `false`. Otherwise, to swap the character at the first index of $pos[x]$ to index $i,ドル all indices of digits less than $x$ must be greater than or equal to the first index of $pos[x]. If this condition is not met, we return `false`. Otherwise, we pop the first index from $pos[x]$ and continue iterating through string $t$.
89
+
90
+
After the iteration, we return `true`.
91
+
92
+
The time complexity is $O(n \times C),ドル and the space complexity is $O(n)$. Here, $n$ is the length of string $s,ドル and $C$ is the size of the digit set, which is 10 in this problem.
83
93
84
94
<!-- tabs:start -->
85
95
Collapse file: solution/1500-1599/1588.Sum of All Odd Length Subarrays/README.md
0 commit comments