Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add solutions to lc problem: No.2414 #3486

Merged
merged 1 commit into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,13 @@ tags:

<!-- solution:start -->

### 方法一:双指针
### 方法一:一次遍历

我们用双指针 $i$ 和 $j$ 分别指向当前连续子字符串的起始位置和结束位置。遍历字符串 $s$,如果当前字符 $s[j]$ 比 $s[j-1]$ 大,则 $j$ 向右移动一位,否则更新 $i$ 为 $j$,并更新最长连续子字符串的长度。
我们可以遍历字符串 $s$,用一个变量 $\textit{ans}$ 记录最长的字母序连续子字符串的长度,用另一个变量 $\textit{cnt}$ 记录当前连续子字符串的长度。初始时 $\textit{ans} = \textit{cnt} = 1$。

接下来,我们从下标为 $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$。

最终返回 $\textit{ans}$ 即可。

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

Expand All @@ -71,14 +75,13 @@ tags:
```python
class Solution:
def longestContinuousSubstring(self, s: str) -> int:
ans = 0
i, j = 0, 1
while j < len(s):
ans = max(ans, j - i)
if ord(s[j]) - ord(s[j - 1]) != 1:
i = j
j += 1
ans = max(ans, j - i)
ans = cnt = 1
for x, y in pairwise(map(ord, s)):
if y - x == 1:
cnt += 1
ans = max(ans, cnt)
else:
cnt = 1
return ans
```

Expand All @@ -87,15 +90,14 @@ class Solution:
```java
class Solution {
public int longestContinuousSubstring(String s) {
int ans = 0;
int i = 0, j = 1;
for (; j < s.length(); ++j) {
ans = Math.max(ans, j - i);
if (s.charAt(j) - s.charAt(j - 1) != 1) {
i = j;
int ans = 1, cnt = 1;
for (int i = 1; i < s.length(); ++i) {
if (s.charAt(i) - s.charAt(i - 1) == 1) {
ans = Math.max(ans, ++cnt);
} else {
cnt = 1;
}
}
ans = Math.max(ans, j - i);
return ans;
}
}
Expand All @@ -107,15 +109,14 @@ class Solution {
class Solution {
public:
int longestContinuousSubstring(string s) {
int ans = 0;
int i = 0, j = 1;
for (; j < s.size(); ++j) {
ans = max(ans, j - i);
if (s[j] - s[j - 1] != 1) {
i = j;
int ans = 1, cnt = 1;
for (int i = 1; i < s.size(); ++i) {
if (s[i] - s[i - 1] == 1) {
ans = max(ans, ++cnt);
} else {
cnt = 1;
}
}
ans = max(ans, j - i);
return ans;
}
};
Expand All @@ -125,15 +126,15 @@ public:

```go
func longestContinuousSubstring(s string) int {
ans := 0
i, j := 0, 1
for ; j < len(s); j++ {
ans = max(ans, j-i)
if s[j]-s[j-1] != 1 {
i = j
ans, cnt := 1, 1
for i := range s[1:] {
if s[i+1]-s[i] == 1 {
cnt++
ans = max(ans, cnt)
} else {
cnt = 1
}
}
ans = max(ans, j-i)
return ans
}
```
Expand All @@ -142,16 +143,15 @@ func longestContinuousSubstring(s string) int {

```ts
function longestContinuousSubstring(s: string): number {
const n = s.length;
let res = 1;
let i = 0;
for (let j = 1; j < n; j++) {
if (s[j].charCodeAt(0) - s[j - 1].charCodeAt(0) !== 1) {
res = Math.max(res, j - i);
i = j;
let [ans, cnt] = [1, 1];
for (let i = 1; i < s.length; ++i) {
if (s.charCodeAt(i) - s.charCodeAt(i - 1) === 1) {
ans = Math.max(ans, ++cnt);
} else {
cnt = 1;
}
}
return Math.max(res, n - i);
return ans;
}
```

Expand All @@ -160,17 +160,18 @@ function longestContinuousSubstring(s: string): number {
```rust
impl Solution {
pub fn longest_continuous_substring(s: String) -> i32 {
let mut ans = 1;
let mut cnt = 1;
let s = s.as_bytes();
let n = s.len();
let mut res = 1;
let mut i = 0;
for j in 1..n {
if s[j] - s[j - 1] != 1 {
res = res.max(j - i);
i = j;
for i in 1..s.len() {
if s[i] - s[i - 1] == 1 {
cnt += 1;
ans = ans.max(cnt);
} else {
cnt = 1;
}
}
res.max(n - i) as i32
ans
}
}
```
Expand All @@ -182,15 +183,16 @@ impl Solution {

int longestContinuousSubstring(char* s) {
int n = strlen(s);
int i = 0;
int res = 1;
for (int j = 1; j < n; j++) {
if (s[j] - s[j - 1] != 1) {
res = max(res, j - i);
i = j;
int ans = 1, cnt = 1;
for (int i = 1; i < n; ++i) {
if (s[i] - s[i - 1] == 1) {
++cnt;
ans = max(ans, cnt);
} else {
cnt = 1;
}
}
return max(res, n - i);
return ans;
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,13 @@ tags:

<!-- solution:start -->

### Solution 1: Two Pointers
### Solution 1: Single Pass

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

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

Finally, we return $\textit{ans}$.

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

Expand All @@ -71,14 +75,13 @@ The time complexity is $O(n)$, where $n$ is the length of the string $s$. The sp
```python
class Solution:
def longestContinuousSubstring(self, s: str) -> int:
ans = 0
i, j = 0, 1
while j < len(s):
ans = max(ans, j - i)
if ord(s[j]) - ord(s[j - 1]) != 1:
i = j
j += 1
ans = max(ans, j - i)
ans = cnt = 1
for x, y in pairwise(map(ord, s)):
if y - x == 1:
cnt += 1
ans = max(ans, cnt)
else:
cnt = 1
return ans
```

Expand All @@ -87,15 +90,14 @@ class Solution:
```java
class Solution {
public int longestContinuousSubstring(String s) {
int ans = 0;
int i = 0, j = 1;
for (; j < s.length(); ++j) {
ans = Math.max(ans, j - i);
if (s.charAt(j) - s.charAt(j - 1) != 1) {
i = j;
int ans = 1, cnt = 1;
for (int i = 1; i < s.length(); ++i) {
if (s.charAt(i) - s.charAt(i - 1) == 1) {
ans = Math.max(ans, ++cnt);
} else {
cnt = 1;
}
}
ans = Math.max(ans, j - i);
return ans;
}
}
Expand All @@ -107,15 +109,14 @@ class Solution {
class Solution {
public:
int longestContinuousSubstring(string s) {
int ans = 0;
int i = 0, j = 1;
for (; j < s.size(); ++j) {
ans = max(ans, j - i);
if (s[j] - s[j - 1] != 1) {
i = j;
int ans = 1, cnt = 1;
for (int i = 1; i < s.size(); ++i) {
if (s[i] - s[i - 1] == 1) {
ans = max(ans, ++cnt);
} else {
cnt = 1;
}
}
ans = max(ans, j - i);
return ans;
}
};
Expand All @@ -125,15 +126,15 @@ public:

```go
func longestContinuousSubstring(s string) int {
ans := 0
i, j := 0, 1
for ; j < len(s); j++ {
ans = max(ans, j-i)
if s[j]-s[j-1] != 1 {
i = j
ans, cnt := 1, 1
for i := range s[1:] {
if s[i+1]-s[i] == 1 {
cnt++
ans = max(ans, cnt)
} else {
cnt = 1
}
}
ans = max(ans, j-i)
return ans
}
```
Expand All @@ -142,16 +143,15 @@ func longestContinuousSubstring(s string) int {

```ts
function longestContinuousSubstring(s: string): number {
const n = s.length;
let res = 1;
let i = 0;
for (let j = 1; j < n; j++) {
if (s[j].charCodeAt(0) - s[j - 1].charCodeAt(0) !== 1) {
res = Math.max(res, j - i);
i = j;
let [ans, cnt] = [1, 1];
for (let i = 1; i < s.length; ++i) {
if (s.charCodeAt(i) - s.charCodeAt(i - 1) === 1) {
ans = Math.max(ans, ++cnt);
} else {
cnt = 1;
}
}
return Math.max(res, n - i);
return ans;
}
```

Expand All @@ -160,17 +160,18 @@ function longestContinuousSubstring(s: string): number {
```rust
impl Solution {
pub fn longest_continuous_substring(s: String) -> i32 {
let mut ans = 1;
let mut cnt = 1;
let s = s.as_bytes();
let n = s.len();
let mut res = 1;
let mut i = 0;
for j in 1..n {
if s[j] - s[j - 1] != 1 {
res = res.max(j - i);
i = j;
for i in 1..s.len() {
if s[i] - s[i - 1] == 1 {
cnt += 1;
ans = ans.max(cnt);
} else {
cnt = 1;
}
}
res.max(n - i) as i32
ans
}
}
```
Expand All @@ -182,15 +183,16 @@ impl Solution {

int longestContinuousSubstring(char* s) {
int n = strlen(s);
int i = 0;
int res = 1;
for (int j = 1; j < n; j++) {
if (s[j] - s[j - 1] != 1) {
res = max(res, j - i);
i = j;
int ans = 1, cnt = 1;
for (int i = 1; i < n; ++i) {
if (s[i] - s[i - 1] == 1) {
++cnt;
ans = max(ans, cnt);
} else {
cnt = 1;
}
}
return max(res, n - i);
return ans;
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

int longestContinuousSubstring(char* s) {
int n = strlen(s);
int i = 0;
int res = 1;
for (int j = 1; j < n; j++) {
if (s[j] - s[j - 1] != 1) {
res = max(res, j - i);
i = j;
int ans = 1, cnt = 1;
for (int i = 1; i < n; ++i) {
if (s[i] - s[i - 1] == 1) {
++cnt;
ans = max(ans, cnt);
} else {
cnt = 1;
}
}
return max(res, n - i);
}
return ans;
}
Loading
Loading