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
feat: add solutions to lc problems: No.1961~1964 (doocs#2145)
* No.1961.Check If String Is a Prefix of Array
* No.1963.Minimum Number of Swaps to Make the String Balanced
* No.1964.Find the Longest Valid Obstacle Course at Each Position
Copy file name to clipboardexpand all lines: solution/1900-1999/1961.Check If String Is a Prefix of Array/README_EN.md
+53-13
Original file line number
Diff line number
Diff line change
@@ -40,18 +40,26 @@ It is impossible to make s using a prefix of arr.</pre>
40
40
41
41
## Solutions
42
42
43
+
**Solution 1: Traversal**
44
+
45
+
We traverse the array $words$, using a variable $t$ to record the currently concatenated string. If the length of $t$ is greater than the length of $s$, it means that $s$ is not a prefix string of $words$, so we return $false$; if the length of $t$ is equal to the length of $s$, we return whether $t$ is equal to $s$.
46
+
47
+
At the end of the traversal, if the length of $t$ is less than the length of $s$, it means that $s$ is not a prefix string of $words$, so we return $false$.
48
+
49
+
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