Skip to content

Commit a776c16

Browse files
authored
feat: add solutions to lc problem: No.2414 (#3486)
No.2414.Length of the Longest Alphabetical Continuous Substring
1 parent e962914 commit a776c16

File tree

9 files changed

+166
-164
lines changed

9 files changed

+166
-164
lines changed

Diff for: solution/2400-2499/2414.Length of the Longest Alphabetical Continuous Substring/README.md

+56-54
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,13 @@ tags:
5858

5959
<!-- solution:start -->
6060

61-
### 方法一:双指针
61+
### 方法一:一次遍历
6262

63-
我们用双指针 $i$ 和 $j$ 分别指向当前连续子字符串的起始位置和结束位置。遍历字符串 $s$,如果当前字符 $s[j]$ 比 $s[j-1]$ 大,则 $j$ 向右移动一位,否则更新 $i$ 为 $j$,并更新最长连续子字符串的长度。
63+
我们可以遍历字符串 $s$,用一个变量 $\textit{ans}$ 记录最长的字母序连续子字符串的长度,用另一个变量 $\textit{cnt}$ 记录当前连续子字符串的长度。初始时 $\textit{ans} = \textit{cnt} = 1$。
64+
65+
接下来,我们从下标为 $1$ 的字符开始遍历字符串 $s$,对于每个字符 $s[i]$,如果 $s[i] - s[i - 1] = 1$,则说明当前字符和前一个字符是连续的,此时 $\textit{cnt} = \textit{cnt} + 1$,并更新 $\textit{ans} = \max(\textit{ans}, \textit{cnt})$;否则,说明当前字符和前一个字符不连续,此时 $\textit{cnt} = 1$。
66+
67+
最终返回 $\textit{ans}$ 即可。
6468

6569
时间复杂度 $O(n)$,其中 $n$ 为字符串 $s$ 的长度。空间复杂度 $O(1)$。
6670

@@ -71,14 +75,13 @@ tags:
7175
```python
7276
class Solution:
7377
def longestContinuousSubstring(self, s: str) -> int:
74-
ans = 0
75-
i, j = 0, 1
76-
while j < len(s):
77-
ans = max(ans, j - i)
78-
if ord(s[j]) - ord(s[j - 1]) != 1:
79-
i = j
80-
j += 1
81-
ans = max(ans, j - i)
78+
ans = cnt = 1
79+
for x, y in pairwise(map(ord, s)):
80+
if y - x == 1:
81+
cnt += 1
82+
ans = max(ans, cnt)
83+
else:
84+
cnt = 1
8285
return ans
8386
```
8487

@@ -87,15 +90,14 @@ class Solution:
8790
```java
8891
class Solution {
8992
public int longestContinuousSubstring(String s) {
90-
int ans = 0;
91-
int i = 0, j = 1;
92-
for (; j < s.length(); ++j) {
93-
ans = Math.max(ans, j - i);
94-
if (s.charAt(j) - s.charAt(j - 1) != 1) {
95-
i = j;
93+
int ans = 1, cnt = 1;
94+
for (int i = 1; i < s.length(); ++i) {
95+
if (s.charAt(i) - s.charAt(i - 1) == 1) {
96+
ans = Math.max(ans, ++cnt);
97+
} else {
98+
cnt = 1;
9699
}
97100
}
98-
ans = Math.max(ans, j - i);
99101
return ans;
100102
}
101103
}
@@ -107,15 +109,14 @@ class Solution {
107109
class Solution {
108110
public:
109111
int longestContinuousSubstring(string s) {
110-
int ans = 0;
111-
int i = 0, j = 1;
112-
for (; j < s.size(); ++j) {
113-
ans = max(ans, j - i);
114-
if (s[j] - s[j - 1] != 1) {
115-
i = j;
112+
int ans = 1, cnt = 1;
113+
for (int i = 1; i < s.size(); ++i) {
114+
if (s[i] - s[i - 1] == 1) {
115+
ans = max(ans, ++cnt);
116+
} else {
117+
cnt = 1;
116118
}
117119
}
118-
ans = max(ans, j - i);
119120
return ans;
120121
}
121122
};
@@ -125,15 +126,15 @@ public:
125126
126127
```go
127128
func longestContinuousSubstring(s string) int {
128-
ans := 0
129-
i, j := 0, 1
130-
for ; j < len(s); j++ {
131-
ans = max(ans, j-i)
132-
if s[j]-s[j-1] != 1 {
133-
i = j
129+
ans, cnt := 1, 1
130+
for i := range s[1:] {
131+
if s[i+1]-s[i] == 1 {
132+
cnt++
133+
ans = max(ans, cnt)
134+
} else {
135+
cnt = 1
134136
}
135137
}
136-
ans = max(ans, j-i)
137138
return ans
138139
}
139140
```
@@ -142,16 +143,15 @@ func longestContinuousSubstring(s string) int {
142143

143144
```ts
144145
function longestContinuousSubstring(s: string): number {
145-
const n = s.length;
146-
let res = 1;
147-
let i = 0;
148-
for (let j = 1; j < n; j++) {
149-
if (s[j].charCodeAt(0) - s[j - 1].charCodeAt(0) !== 1) {
150-
res = Math.max(res, j - i);
151-
i = j;
146+
let [ans, cnt] = [1, 1];
147+
for (let i = 1; i < s.length; ++i) {
148+
if (s.charCodeAt(i) - s.charCodeAt(i - 1) === 1) {
149+
ans = Math.max(ans, ++cnt);
150+
} else {
151+
cnt = 1;
152152
}
153153
}
154-
return Math.max(res, n - i);
154+
return ans;
155155
}
156156
```
157157

@@ -160,17 +160,18 @@ function longestContinuousSubstring(s: string): number {
160160
```rust
161161
impl Solution {
162162
pub fn longest_continuous_substring(s: String) -> i32 {
163+
let mut ans = 1;
164+
let mut cnt = 1;
163165
let s = s.as_bytes();
164-
let n = s.len();
165-
let mut res = 1;
166-
let mut i = 0;
167-
for j in 1..n {
168-
if s[j] - s[j - 1] != 1 {
169-
res = res.max(j - i);
170-
i = j;
166+
for i in 1..s.len() {
167+
if s[i] - s[i - 1] == 1 {
168+
cnt += 1;
169+
ans = ans.max(cnt);
170+
} else {
171+
cnt = 1;
171172
}
172173
}
173-
res.max(n - i) as i32
174+
ans
174175
}
175176
}
176177
```
@@ -182,15 +183,16 @@ impl Solution {
182183

183184
int longestContinuousSubstring(char* s) {
184185
int n = strlen(s);
185-
int i = 0;
186-
int res = 1;
187-
for (int j = 1; j < n; j++) {
188-
if (s[j] - s[j - 1] != 1) {
189-
res = max(res, j - i);
190-
i = j;
186+
int ans = 1, cnt = 1;
187+
for (int i = 1; i < n; ++i) {
188+
if (s[i] - s[i - 1] == 1) {
189+
++cnt;
190+
ans = max(ans, cnt);
191+
} else {
192+
cnt = 1;
191193
}
192194
}
193-
return max(res, n - i);
195+
return ans;
194196
}
195197
```
196198

Diff for: solution/2400-2499/2414.Length of the Longest Alphabetical Continuous Substring/README_EN.md

+56-54
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,13 @@ tags:
5858

5959
<!-- solution:start -->
6060

61-
### Solution 1: Two Pointers
61+
### Solution 1: Single Pass
6262

63-
We use two pointers $i$ and $j$ to point to the start and end of the current consecutive substring respectively. Traverse the string $s$, if the current character $s[j]$ is greater than $s[j-1]$, then move $j$ one step to the right, otherwise update $i$ to $j$, and update the length of the longest consecutive substring.
63+
We can traverse the string $s$ and use a variable $\textit{ans}$ to record the length of the longest lexicographically consecutive substring, and another variable $\textit{cnt}$ to record the length of the current consecutive substring. Initially, $\textit{ans} = \textit{cnt} = 1$.
64+
65+
Next, we start traversing the string $s$ from the character at index $1$. For each character $s[i]$, if $s[i] - s[i - 1] = 1$, it means the current character and the previous character are consecutive. In this case, $\textit{cnt} = \textit{cnt} + 1$, and we update $\textit{ans} = \max(\textit{ans}, \textit{cnt})$. Otherwise, it means the current character and the previous character are not consecutive, so $\textit{cnt} = 1$.
66+
67+
Finally, we return $\textit{ans}$.
6468

6569
The time complexity is $O(n)$, where $n$ is the length of the string $s$. The space complexity is $O(1)$.
6670

@@ -71,14 +75,13 @@ The time complexity is $O(n)$, where $n$ is the length of the string $s$. The sp
7175
```python
7276
class Solution:
7377
def longestContinuousSubstring(self, s: str) -> int:
74-
ans = 0
75-
i, j = 0, 1
76-
while j < len(s):
77-
ans = max(ans, j - i)
78-
if ord(s[j]) - ord(s[j - 1]) != 1:
79-
i = j
80-
j += 1
81-
ans = max(ans, j - i)
78+
ans = cnt = 1
79+
for x, y in pairwise(map(ord, s)):
80+
if y - x == 1:
81+
cnt += 1
82+
ans = max(ans, cnt)
83+
else:
84+
cnt = 1
8285
return ans
8386
```
8487

@@ -87,15 +90,14 @@ class Solution:
8790
```java
8891
class Solution {
8992
public int longestContinuousSubstring(String s) {
90-
int ans = 0;
91-
int i = 0, j = 1;
92-
for (; j < s.length(); ++j) {
93-
ans = Math.max(ans, j - i);
94-
if (s.charAt(j) - s.charAt(j - 1) != 1) {
95-
i = j;
93+
int ans = 1, cnt = 1;
94+
for (int i = 1; i < s.length(); ++i) {
95+
if (s.charAt(i) - s.charAt(i - 1) == 1) {
96+
ans = Math.max(ans, ++cnt);
97+
} else {
98+
cnt = 1;
9699
}
97100
}
98-
ans = Math.max(ans, j - i);
99101
return ans;
100102
}
101103
}
@@ -107,15 +109,14 @@ class Solution {
107109
class Solution {
108110
public:
109111
int longestContinuousSubstring(string s) {
110-
int ans = 0;
111-
int i = 0, j = 1;
112-
for (; j < s.size(); ++j) {
113-
ans = max(ans, j - i);
114-
if (s[j] - s[j - 1] != 1) {
115-
i = j;
112+
int ans = 1, cnt = 1;
113+
for (int i = 1; i < s.size(); ++i) {
114+
if (s[i] - s[i - 1] == 1) {
115+
ans = max(ans, ++cnt);
116+
} else {
117+
cnt = 1;
116118
}
117119
}
118-
ans = max(ans, j - i);
119120
return ans;
120121
}
121122
};
@@ -125,15 +126,15 @@ public:
125126
126127
```go
127128
func longestContinuousSubstring(s string) int {
128-
ans := 0
129-
i, j := 0, 1
130-
for ; j < len(s); j++ {
131-
ans = max(ans, j-i)
132-
if s[j]-s[j-1] != 1 {
133-
i = j
129+
ans, cnt := 1, 1
130+
for i := range s[1:] {
131+
if s[i+1]-s[i] == 1 {
132+
cnt++
133+
ans = max(ans, cnt)
134+
} else {
135+
cnt = 1
134136
}
135137
}
136-
ans = max(ans, j-i)
137138
return ans
138139
}
139140
```
@@ -142,16 +143,15 @@ func longestContinuousSubstring(s string) int {
142143

143144
```ts
144145
function longestContinuousSubstring(s: string): number {
145-
const n = s.length;
146-
let res = 1;
147-
let i = 0;
148-
for (let j = 1; j < n; j++) {
149-
if (s[j].charCodeAt(0) - s[j - 1].charCodeAt(0) !== 1) {
150-
res = Math.max(res, j - i);
151-
i = j;
146+
let [ans, cnt] = [1, 1];
147+
for (let i = 1; i < s.length; ++i) {
148+
if (s.charCodeAt(i) - s.charCodeAt(i - 1) === 1) {
149+
ans = Math.max(ans, ++cnt);
150+
} else {
151+
cnt = 1;
152152
}
153153
}
154-
return Math.max(res, n - i);
154+
return ans;
155155
}
156156
```
157157

@@ -160,17 +160,18 @@ function longestContinuousSubstring(s: string): number {
160160
```rust
161161
impl Solution {
162162
pub fn longest_continuous_substring(s: String) -> i32 {
163+
let mut ans = 1;
164+
let mut cnt = 1;
163165
let s = s.as_bytes();
164-
let n = s.len();
165-
let mut res = 1;
166-
let mut i = 0;
167-
for j in 1..n {
168-
if s[j] - s[j - 1] != 1 {
169-
res = res.max(j - i);
170-
i = j;
166+
for i in 1..s.len() {
167+
if s[i] - s[i - 1] == 1 {
168+
cnt += 1;
169+
ans = ans.max(cnt);
170+
} else {
171+
cnt = 1;
171172
}
172173
}
173-
res.max(n - i) as i32
174+
ans
174175
}
175176
}
176177
```
@@ -182,15 +183,16 @@ impl Solution {
182183

183184
int longestContinuousSubstring(char* s) {
184185
int n = strlen(s);
185-
int i = 0;
186-
int res = 1;
187-
for (int j = 1; j < n; j++) {
188-
if (s[j] - s[j - 1] != 1) {
189-
res = max(res, j - i);
190-
i = j;
186+
int ans = 1, cnt = 1;
187+
for (int i = 1; i < n; ++i) {
188+
if (s[i] - s[i - 1] == 1) {
189+
++cnt;
190+
ans = max(ans, cnt);
191+
} else {
192+
cnt = 1;
191193
}
192194
}
193-
return max(res, n - i);
195+
return ans;
194196
}
195197
```
196198

Diff for: solution/2400-2499/2414.Length of the Longest Alphabetical Continuous Substring/Solution.c

+9-8
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22

33
int longestContinuousSubstring(char* s) {
44
int n = strlen(s);
5-
int i = 0;
6-
int res = 1;
7-
for (int j = 1; j < n; j++) {
8-
if (s[j] - s[j - 1] != 1) {
9-
res = max(res, j - i);
10-
i = j;
5+
int ans = 1, cnt = 1;
6+
for (int i = 1; i < n; ++i) {
7+
if (s[i] - s[i - 1] == 1) {
8+
++cnt;
9+
ans = max(ans, cnt);
10+
} else {
11+
cnt = 1;
1112
}
1213
}
13-
return max(res, n - i);
14-
}
14+
return ans;
15+
}

0 commit comments

Comments
 (0)