diff --git a/solution/2400-2499/2414.Length of the Longest Alphabetical Continuous Substring/README.md b/solution/2400-2499/2414.Length of the Longest Alphabetical Continuous Substring/README.md index c13ffe7a93a2c..f70943b64ef77 100644 --- a/solution/2400-2499/2414.Length of the Longest Alphabetical Continuous Substring/README.md +++ b/solution/2400-2499/2414.Length of the Longest Alphabetical Continuous Substring/README.md @@ -58,9 +58,13 @@ tags: -### 方法一:双指针 +### 方法一:一次遍历 -我们用双指针 $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)$。 @@ -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 ``` @@ -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; } } @@ -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; } }; @@ -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 } ``` @@ -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; } ``` @@ -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 } } ``` @@ -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; } ``` diff --git a/solution/2400-2499/2414.Length of the Longest Alphabetical Continuous Substring/README_EN.md b/solution/2400-2499/2414.Length of the Longest Alphabetical Continuous Substring/README_EN.md index e5b0d9799fcb1..4f7e9a104ccb2 100644 --- a/solution/2400-2499/2414.Length of the Longest Alphabetical Continuous Substring/README_EN.md +++ b/solution/2400-2499/2414.Length of the Longest Alphabetical Continuous Substring/README_EN.md @@ -58,9 +58,13 @@ tags: -### 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)$. @@ -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 ``` @@ -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; } } @@ -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; } }; @@ -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 } ``` @@ -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; } ``` @@ -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 } } ``` @@ -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; } ``` diff --git a/solution/2400-2499/2414.Length of the Longest Alphabetical Continuous Substring/Solution.c b/solution/2400-2499/2414.Length of the Longest Alphabetical Continuous Substring/Solution.c index 3539ddbb3e57c..4b6b18554791f 100644 --- a/solution/2400-2499/2414.Length of the Longest Alphabetical Continuous Substring/Solution.c +++ b/solution/2400-2499/2414.Length of the Longest Alphabetical Continuous Substring/Solution.c @@ -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); -} \ No newline at end of file + return ans; +} diff --git a/solution/2400-2499/2414.Length of the Longest Alphabetical Continuous Substring/Solution.cpp b/solution/2400-2499/2414.Length of the Longest Alphabetical Continuous Substring/Solution.cpp index 74085d1710648..f881f49ad274a 100644 --- a/solution/2400-2499/2414.Length of the Longest Alphabetical Continuous Substring/Solution.cpp +++ b/solution/2400-2499/2414.Length of the Longest Alphabetical Continuous Substring/Solution.cpp @@ -1,15 +1,14 @@ 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; } -}; \ No newline at end of file +}; diff --git a/solution/2400-2499/2414.Length of the Longest Alphabetical Continuous Substring/Solution.go b/solution/2400-2499/2414.Length of the Longest Alphabetical Continuous Substring/Solution.go index 4fd1f91e83dde..56ff573e6cb43 100644 --- a/solution/2400-2499/2414.Length of the Longest Alphabetical Continuous Substring/Solution.go +++ b/solution/2400-2499/2414.Length of the Longest Alphabetical Continuous Substring/Solution.go @@ -1,12 +1,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, 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 -} \ No newline at end of file +} diff --git a/solution/2400-2499/2414.Length of the Longest Alphabetical Continuous Substring/Solution.java b/solution/2400-2499/2414.Length of the Longest Alphabetical Continuous Substring/Solution.java index e3bd594b28189..3a02686780e68 100644 --- a/solution/2400-2499/2414.Length of the Longest Alphabetical Continuous Substring/Solution.java +++ b/solution/2400-2499/2414.Length of the Longest Alphabetical Continuous Substring/Solution.java @@ -1,14 +1,13 @@ 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; } -} \ No newline at end of file +} diff --git a/solution/2400-2499/2414.Length of the Longest Alphabetical Continuous Substring/Solution.py b/solution/2400-2499/2414.Length of the Longest Alphabetical Continuous Substring/Solution.py index a5796dc517f10..6a0a298820d6a 100644 --- a/solution/2400-2499/2414.Length of the Longest Alphabetical Continuous Substring/Solution.py +++ b/solution/2400-2499/2414.Length of the Longest Alphabetical Continuous Substring/Solution.py @@ -1,11 +1,10 @@ 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 diff --git a/solution/2400-2499/2414.Length of the Longest Alphabetical Continuous Substring/Solution.rs b/solution/2400-2499/2414.Length of the Longest Alphabetical Continuous Substring/Solution.rs index bf6f031db58ea..81e53a6fbc1b4 100644 --- a/solution/2400-2499/2414.Length of the Longest Alphabetical Continuous Substring/Solution.rs +++ b/solution/2400-2499/2414.Length of the Longest Alphabetical Continuous Substring/Solution.rs @@ -1,15 +1,16 @@ 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 } } diff --git a/solution/2400-2499/2414.Length of the Longest Alphabetical Continuous Substring/Solution.ts b/solution/2400-2499/2414.Length of the Longest Alphabetical Continuous Substring/Solution.ts index f4bc49942d4c6..f201de1fa0df9 100644 --- a/solution/2400-2499/2414.Length of the Longest Alphabetical Continuous Substring/Solution.ts +++ b/solution/2400-2499/2414.Length of the Longest Alphabetical Continuous Substring/Solution.ts @@ -1,12 +1,11 @@ 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; }