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/2100-2199/2108.Find First Palindromic String in the Array/README_EN.md
+20-39
Original file line number
Diff line number
Diff line change
@@ -65,7 +65,13 @@ Note that "racecar" is also palindromic, but it is not the first.
65
65
66
66
<!-- solution:start -->
67
67
68
-
### Solution 1
68
+
### Solution 1: Simulation
69
+
70
+
We iterate through the array `words`, for each string `w`, we determine if it is a palindrome. If it is, then we return `w`; otherwise, we continue to iterate.
71
+
72
+
To determine if a string is a palindrome, we can use two pointers, one pointing to the start and the other to the end of the string, moving towards the center, and checking if the corresponding characters are equal. If, after traversing the entire string, no unequal characters are found, then the string is a palindrome.
73
+
74
+
The time complexity is $O(L)$, where $L$ is the sum of the lengths of all strings in the array `words`. The space complexity is $O(1)$.
Copy file name to clipboardExpand all lines: solution/2100-2199/2109.Adding Spaces to a String/README_EN.md
+11-33
Original file line number
Diff line number
Diff line change
@@ -76,7 +76,11 @@ We are also able to place spaces before the first character of the string.
76
76
77
77
<!-- solution:start -->
78
78
79
-
### Solution 1
79
+
### Solution 1: Two Pointers
80
+
81
+
We can use two pointers $i$ and $j$ to point to the beginning of the string $s$ and the array $\text{spaces}$, respectively. Then, we iterate through the string $s$ from the beginning to the end. When $i$ equals $\text{spaces}[j]$, we add a space to the result string, and then increment $j$ by $1$. Next, we add $s[i]$ to the result string, and then increment $i$ by $1$. We continue this process until we have iterated through the entire string $s$.
82
+
83
+
The time complexity is $O(n + m)$, and the space complexity is $O(n + m)$, where $n$ and $m$ are the lengths of the string $s$ and the array $spaces$, respectively.
0 commit comments