跳转至

2414. 最长的字母序连续子字符串的长度

题目描述

字母序连续字符串 是由字母表中连续字母组成的字符串。换句话说,字符串 "abcdefghijklmnopqrstuvwxyz" 的任意子字符串都是 字母序连续字符串

  • 例如,"abc" 是一个字母序连续字符串,而 "acb""za" 不是。

给你一个仅由小写英文字母组成的字符串 s ,返回其 最长 的 字母序连续子字符串 的长度。

 

示例 1:

输入:s = "abacaba"
输出:2
解释:共有 4 个不同的字母序连续子字符串 "a"、"b"、"c" 和 "ab" 。
"ab" 是最长的字母序连续子字符串。

示例 2:

输入:s = "abcde"
输出:5
解释:"abcde" 是最长的字母序连续子字符串。

 

提示:

  • 1 <= s.length <= 105
  • s 由小写英文字母组成

解法

方法一:双指针

我们用双指针 $i$ 和 $j$ 分别指向当前连续子字符串的起始位置和结束位置。遍历字符串 $s$,如果当前字符 $s[j]$ 比 $s[j-1]$ 大,则 $j$ 向右移动一位,否则更新 $i$ 为 $j$,并更新最长连续子字符串的长度。

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
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)
        return ans
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
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;
            }
        }
        ans = Math.max(ans, j - i);
        return ans;
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
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;
            }
        }
        ans = max(ans, j - i);
        return ans;
    }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
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 = max(ans, j-i)
    return ans
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
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;
        }
    }
    return Math.max(res, n - i);
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
impl Solution {
    pub fn longest_continuous_substring(s: String) -> i32 {
        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;
            }
        }
        res.max(n - i) as i32
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
#define max(a, b) (((a) > (b)) ? (a) : (b))

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;
        }
    }
    return max(res, n - i);
}

评论