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

最长的字母序连续子字符串的长度 #161

Open
yankewei opened this issue Sep 20, 2022 · 1 comment
Open

最长的字母序连续子字符串的长度 #161

yankewei opened this issue Sep 20, 2022 · 1 comment
Labels
中等 题目难度为中等 字符串 题目类型为字符串

Comments

@yankewei
Copy link
Owner

字母序连续字符串是由字母表中连续字母组成的字符串。换句话说,字符串 "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 由小写英文字母组成

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/length-of-the-longest-alphabetical-continuous-substring
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

@yankewei yankewei added 中等 题目难度为中等 字符串 题目类型为字符串 labels Sep 20, 2022
@yankewei
Copy link
Owner Author

模拟

class Solution {

    /**
     * @param String $s
     * @return Integer
     */
    function longestContinuousSubstring($s) {
        $result = 1;
        $consequent[] = $s[$i];
        
        for ($i = 0; $i < strlen($s); $i++) {
            if (ord($consequent[count($consequent)-1]) + 1 === ord($s[$i])) {
                $consequent[] = $s[$i];
                continue;
            }
            
            $result = count($consequent) > $result ? count($consequent) : $result;
            $consequent = [];
            $consequent[] = $s[$i];
        }
        
        return count($consequent) > $result ? count($consequent) : $result;
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
中等 题目难度为中等 字符串 题目类型为字符串
Projects
None yet
Development

No branches or pull requests

1 participant