Skip to content

Commit ae5bc33

Browse files
authored
feat: update solutions to lc problems: No.2108,2109 (doocs#3148)
* No.2108.Find First Palindromic String in the Array * No.2109.Adding Spaces to a String
1 parent 7c1baa8 commit ae5bc33

File tree

9 files changed

+73
-201
lines changed

9 files changed

+73
-201
lines changed

Diff for: solution/2100-2199/2108.Find First Palindromic String in the Array/README.md

+15-40
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,11 @@ tags:
6666

6767
### 方法一:模拟
6868

69-
遍历数组 `words`,对于每个字符串 `w`,判断其是否为回文字符串,如果是,则返回 `w`,否则继续遍历。
69+
我们遍历数组 `words`,对于每个字符串 `w`,判断其是否为回文字符串,如果是,则返回 `w`,否则继续遍历。
7070

7171
判断一个字符串是否为回文字符串,可以使用双指针,分别指向字符串的首尾,向中间移动,判断对应的字符是否相等。如果遍历完整个字符串,都没有发现不相等的字符,则该字符串为回文字符串。
7272

73-
时间复杂度 $O(L)$,空间复杂度 $O(1)$,其中 $L$ 为数组 `words` 中所有字符串的长度之和。
73+
时间复杂度 $O(L)$,其中 $L$ 为数组 `words` 中所有字符串的长度之和。空间复杂度 $O(1)$
7474

7575
<!-- tabs:start -->
7676

@@ -148,21 +148,7 @@ func firstPalindrome(words []string) string {
148148

149149
```ts
150150
function firstPalindrome(words: string[]): string {
151-
for (const word of words) {
152-
let left = 0;
153-
let right = word.length - 1;
154-
while (left < right) {
155-
if (word[left] !== word[right]) {
156-
break;
157-
}
158-
left++;
159-
right--;
160-
}
161-
if (left >= right) {
162-
return word;
163-
}
164-
}
165-
return '';
151+
return words.find(w => w === w.split('').reverse().join('')) || '';
166152
}
167153
```
168154

@@ -171,19 +157,9 @@ function firstPalindrome(words: string[]): string {
171157
```rust
172158
impl Solution {
173159
pub fn first_palindrome(words: Vec<String>) -> String {
174-
for word in words.iter() {
175-
let s = word.as_bytes();
176-
let mut left = 0;
177-
let mut right = s.len() - 1;
178-
while left < right {
179-
if s[left] != s[right] {
180-
break;
181-
}
182-
left += 1;
183-
right -= 1;
184-
}
185-
if left >= right {
186-
return word.clone();
160+
for w in words {
161+
if w == w.chars().rev().collect::<String>() {
162+
return w;
187163
}
188164
}
189165
String::new()
@@ -195,18 +171,17 @@ impl Solution {
195171

196172
```c
197173
char* firstPalindrome(char** words, int wordsSize) {
198-
for (int i = 0; i < wordsSize; i++) {
199-
int left = 0;
200-
int right = strlen(words[i]) - 1;
201-
while (left < right) {
202-
if (words[i][left] != words[i][right]) {
203-
break;
174+
for (int i = 0; i < wordsSize; ++i) {
175+
char* w = words[i];
176+
int len = strlen(w);
177+
bool ok = true;
178+
for (int j = 0, k = len - 1; j < k && ok; ++j, --k) {
179+
if (w[j] != w[k]) {
180+
ok = false;
204181
}
205-
left++;
206-
right--;
207182
}
208-
if (left >= right) {
209-
return words[i];
183+
if (ok) {
184+
return w;
210185
}
211186
}
212187
return "";

Diff for: solution/2100-2199/2108.Find First Palindromic String in the Array/README_EN.md

+20-39
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,13 @@ Note that &quot;racecar&quot; is also palindromic, but it is not the first.
6565

6666
<!-- solution:start -->
6767

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)$.
6975

7076
<!-- tabs:start -->
7177

@@ -143,21 +149,7 @@ func firstPalindrome(words []string) string {
143149

144150
```ts
145151
function firstPalindrome(words: string[]): string {
146-
for (const word of words) {
147-
let left = 0;
148-
let right = word.length - 1;
149-
while (left < right) {
150-
if (word[left] !== word[right]) {
151-
break;
152-
}
153-
left++;
154-
right--;
155-
}
156-
if (left >= right) {
157-
return word;
158-
}
159-
}
160-
return '';
152+
return words.find(w => w === w.split('').reverse().join('')) || '';
161153
}
162154
```
163155

@@ -166,19 +158,9 @@ function firstPalindrome(words: string[]): string {
166158
```rust
167159
impl Solution {
168160
pub fn first_palindrome(words: Vec<String>) -> String {
169-
for word in words.iter() {
170-
let s = word.as_bytes();
171-
let mut left = 0;
172-
let mut right = s.len() - 1;
173-
while left < right {
174-
if s[left] != s[right] {
175-
break;
176-
}
177-
left += 1;
178-
right -= 1;
179-
}
180-
if left >= right {
181-
return word.clone();
161+
for w in words {
162+
if w == w.chars().rev().collect::<String>() {
163+
return w;
182164
}
183165
}
184166
String::new()
@@ -190,18 +172,17 @@ impl Solution {
190172

191173
```c
192174
char* firstPalindrome(char** words, int wordsSize) {
193-
for (int i = 0; i < wordsSize; i++) {
194-
int left = 0;
195-
int right = strlen(words[i]) - 1;
196-
while (left < right) {
197-
if (words[i][left] != words[i][right]) {
198-
break;
175+
for (int i = 0; i < wordsSize; ++i) {
176+
char* w = words[i];
177+
int len = strlen(w);
178+
bool ok = true;
179+
for (int j = 0, k = len - 1; j < k && ok; ++j, --k) {
180+
if (w[j] != w[k]) {
181+
ok = false;
199182
}
200-
left++;
201-
right--;
202183
}
203-
if (left >= right) {
204-
return words[i];
184+
if (ok) {
185+
return w;
205186
}
206187
}
207188
return "";

Diff for: solution/2100-2199/2108.Find First Palindromic String in the Array/Solution.c

+9-10
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
char* firstPalindrome(char** words, int wordsSize) {
2-
for (int i = 0; i < wordsSize; i++) {
3-
int left = 0;
4-
int right = strlen(words[i]) - 1;
5-
while (left < right) {
6-
if (words[i][left] != words[i][right]) {
7-
break;
2+
for (int i = 0; i < wordsSize; ++i) {
3+
char* w = words[i];
4+
int len = strlen(w);
5+
bool ok = true;
6+
for (int j = 0, k = len - 1; j < k && ok; ++j, --k) {
7+
if (w[j] != w[k]) {
8+
ok = false;
89
}
9-
left++;
10-
right--;
1110
}
12-
if (left >= right) {
13-
return words[i];
11+
if (ok) {
12+
return w;
1413
}
1514
}
1615
return "";

Diff for: solution/2100-2199/2108.Find First Palindromic String in the Array/Solution.rs

+3-13
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,8 @@
11
impl Solution {
22
pub fn first_palindrome(words: Vec<String>) -> String {
3-
for word in words.iter() {
4-
let s = word.as_bytes();
5-
let mut left = 0;
6-
let mut right = s.len() - 1;
7-
while left < right {
8-
if s[left] != s[right] {
9-
break;
10-
}
11-
left += 1;
12-
right -= 1;
13-
}
14-
if left >= right {
15-
return word.clone();
3+
for w in words {
4+
if w == w.chars().rev().collect::<String>() {
5+
return w;
166
}
177
}
188
String::new()
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,3 @@
11
function firstPalindrome(words: string[]): string {
2-
for (const word of words) {
3-
let left = 0;
4-
let right = word.length - 1;
5-
while (left < right) {
6-
if (word[left] !== word[right]) {
7-
break;
8-
}
9-
left++;
10-
right--;
11-
}
12-
if (left >= right) {
13-
return word;
14-
}
15-
}
16-
return '';
2+
return words.find(w => w === w.split('').reverse().join('')) || '';
173
}

Diff for: solution/2100-2199/2109.Adding Spaces to a String/README.md

+8-34
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,9 @@ tags:
8282

8383
### 方法一:双指针
8484

85-
我们可以用双指针 $i$ 和 $j$ 分别指向字符串 $s$ 和数组 $spaces$ 的头部,然后从头到尾遍历字符串 $s$,当 $i$ 等于 $spaces[j]$ 时,我们往结果字符串中添加一个空格,然后 $j$ 自增 1。接下来,我们将 $s[i]$ 添加到结果字符串中,然后 $i$ 自增 1。继续这个过程,直到遍历完字符串 $s$。
85+
我们可以用双指针 $i$ 和 $j$ 分别指向字符串 $s$ 和数组 $\text{spaces}$ 的头部,然后从头到尾遍历字符串 $s$,当 $i$ 等于 $\text{spaces}[j]$ 时,我们往结果字符串中添加一个空格,然后 $j$ 自增 $1$。接下来,我们将 $s[i]$ 添加到结果字符串中,然后 $i$ 自增 $1$。继续这个过程,直到遍历完字符串 $s$。
8686

87-
时间复杂度 $O(n + m)$,其中 $n$ 和 $m$ 分别是字符串 $s$ 和数组 $spaces$ 的长度。忽略答案的空间消耗,空间复杂度 $O(1)$
87+
时间复杂度 $O(n + m)$,空间复杂度 $O(n + m)$。其中 $n$ 和 $m$ 分别是字符串 $s$ 和数组 $spaces$ 的长度。
8888

8989
<!-- tabs:start -->
9090

@@ -160,46 +160,20 @@ func addSpaces(s string, spaces []int) string {
160160

161161
```ts
162162
function addSpaces(s: string, spaces: number[]): string {
163-
let ans = '';
163+
const ans: string[] = [];
164164
for (let i = 0, j = 0; i < s.length; i++) {
165-
if (j < spaces.length && i === spaces[j]) {
166-
ans += ' ';
167-
++j;
165+
if (i === spaces[j]) {
166+
ans.push(' ');
167+
j++;
168168
}
169-
ans += s[i];
169+
ans.push(s[i]);
170170
}
171-
return ans;
171+
return ans.join('');
172172
}
173173
```
174174

175175
<!-- tabs:end -->
176176

177177
<!-- solution:end -->
178178

179-
<!-- solution:start -->
180-
181-
### 方法二
182-
183-
<!-- tabs:start -->
184-
185-
#### Python3
186-
187-
```python
188-
class Solution:
189-
def addSpaces(self, s: str, spaces: List[int]) -> str:
190-
ans = []
191-
i, j = len(s) - 1, len(spaces) - 1
192-
while i >= 0:
193-
ans.append(s[i])
194-
if j >= 0 and i == spaces[j]:
195-
ans.append(' ')
196-
j -= 1
197-
i -= 1
198-
return ''.join(ans[::-1])
199-
```
200-
201-
<!-- tabs:end -->
202-
203-
<!-- solution:end -->
204-
205179
<!-- problem:end -->

Diff for: solution/2100-2199/2109.Adding Spaces to a String/README_EN.md

+11-33
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,11 @@ We are also able to place spaces before the first character of the string.
7676

7777
<!-- solution:start -->
7878

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.
8084

8185
<!-- tabs:start -->
8286

@@ -152,46 +156,20 @@ func addSpaces(s string, spaces []int) string {
152156

153157
```ts
154158
function addSpaces(s: string, spaces: number[]): string {
155-
let ans = '';
159+
const ans: string[] = [];
156160
for (let i = 0, j = 0; i < s.length; i++) {
157-
if (j < spaces.length && i === spaces[j]) {
158-
ans += ' ';
159-
++j;
161+
if (i === spaces[j]) {
162+
ans.push(' ');
163+
j++;
160164
}
161-
ans += s[i];
165+
ans.push(s[i]);
162166
}
163-
return ans;
167+
return ans.join('');
164168
}
165169
```
166170

167171
<!-- tabs:end -->
168172

169173
<!-- solution:end -->
170174

171-
<!-- solution:start -->
172-
173-
### Solution 2
174-
175-
<!-- tabs:start -->
176-
177-
#### Python3
178-
179-
```python
180-
class Solution:
181-
def addSpaces(self, s: str, spaces: List[int]) -> str:
182-
ans = []
183-
i, j = len(s) - 1, len(spaces) - 1
184-
while i >= 0:
185-
ans.append(s[i])
186-
if j >= 0 and i == spaces[j]:
187-
ans.append(' ')
188-
j -= 1
189-
i -= 1
190-
return ''.join(ans[::-1])
191-
```
192-
193-
<!-- tabs:end -->
194-
195-
<!-- solution:end -->
196-
197175
<!-- problem:end -->
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
function addSpaces(s: string, spaces: number[]): string {
2-
let ans = '';
2+
const ans: string[] = [];
33
for (let i = 0, j = 0; i < s.length; i++) {
4-
if (j < spaces.length && i === spaces[j]) {
5-
ans += ' ';
6-
++j;
4+
if (i === spaces[j]) {
5+
ans.push(' ');
6+
j++;
77
}
8-
ans += s[i];
8+
ans.push(s[i]);
99
}
10-
return ans;
10+
return ans.join('');
1111
}

0 commit comments

Comments
 (0)