2414. Length of the Longest Alphabetical Continuous Substring LeetCode Solution
In this guide, you will get 2414. Length of the Longest Alphabetical Continuous Substring LeetCode Solution with the best time and space complexity. The solution to Length of the Longest Alphabetical Continuous Substring problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Length of the Longest Alphabetical Continuous Substring solution in C++
- Length of the Longest Alphabetical Continuous Substring solution in Java
- Length of the Longest Alphabetical Continuous Substring solution in Python
- Additional Resources
Problem Statement of Length of the Longest Alphabetical Continuous Substring
An alphabetical continuous string is a string consisting of consecutive letters in the alphabet. In other words, it is any substring of the string “abcdefghijklmnopqrstuvwxyz”.
For example, “abc” is an alphabetical continuous string, while “acb” and “za” are not.
Given a string s consisting of lowercase letters only, return the length of the longest alphabetical continuous substring.
Example 1:
Input: s = “abacaba”
Output: 2
Explanation: There are 4 distinct continuous substrings: “a”, “b”, “c” and “ab”.
“ab” is the longest continuous substring.
Example 2:
Input: s = “abcde”
Output: 5
Explanation: “abcde” is the longest continuous substring.
Constraints:
1 <= s.length <= 105
s consists of only English lowercase letters.
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(1)
2414. Length of the Longest Alphabetical Continuous Substring LeetCode Solution in C++
class Solution {
public:
int longestContinuousSubstring(string s) {
int ans = 1;
int runningLen = 1;
for (int i = 1; i < s.length(); ++i)
if (s[i] == s[i - 1] + 1)
ans = max(ans, ++runningLen);
else
runningLen = 1;
return ans;
}
};
/* code provided by PROGIEZ */
2414. Length of the Longest Alphabetical Continuous Substring LeetCode Solution in Java
class Solution {
public int longestContinuousSubstring(String s) {
int ans = 1;
int runningLen = 1;
for (int i = 1; i < s.length(); ++i)
if (s.charAt(i) == s.charAt(i - 1) + 1)
ans = Math.max(ans, ++runningLen);
else
runningLen = 1;
return ans;
}
}
// code provided by PROGIEZ
2414. Length of the Longest Alphabetical Continuous Substring LeetCode Solution in Python
class Solution:
def longestContinuousSubstring(self, s: str) -> int:
ans = 1
runningLen = 1
for a, b in zip(s, s[1:]):
if ord(a) + 1 == ord(b):
runningLen += 1
ans = max(ans, runningLen)
else:
runningLen = 1
return ans
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.