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/0900-0999/0942.DI String Match/README_EN.md
+35-33Lines changed: 35 additions & 33 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -53,7 +53,13 @@ tags:
53
53
54
54
<!-- solution:start -->
55
55
56
-
### Solution 1
56
+
### Solution 1: Greedy Algorithm
57
+
58
+
We can use two pointers `low` and `high` to represent the current minimum and maximum values, respectively. Then, we traverse the string `s`. If the current character is `I`, we add `low` to the result array, and increment `low` by 1; if the current character is `D`, we add `high` to the result array, and decrement `high` by 1.
59
+
60
+
Finally, we add `low` to the result array and return the result array.
61
+
62
+
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