Welcome to Subscribe On Youtube

Formatted question description: https://leetcode.ca/all/2414.html

2414. Length of the Longest Alphabetical Continuous Substring

  • Difficulty: Medium.
  • Related Topics: .
  • Similar Questions: Longest Consecutive Sequence, Arithmetic Slices, Max Consecutive Ones, Maximum Number of Vowels in a Substring of Given Length, Number of Zero-Filled Subarrays.

Problem

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.

Solution (Java, C++, Python)

  • class Solution {
        public int longestContinuousSubstring(String s) {
            int x = 0, len = 0, res = 0;
            for(char ch : s.toCharArray()) {
                len = ch-'a'-x==1 ? len+1 : 1;  // add to len or restart
                res = Math.max(res, len);
                x = ch-'a';
            }
            return res;
        }
    }
    
    ############
    
    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;
        }
    }
    
  • 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
    
    ############
    
    # 2414. Length of the Longest Alphabetical Continuous Substring
    # https://leetcode.com/problems/length-of-the-longest-alphabetical-continuous-substring/
    
    class Solution:
        def longestContinuousSubstring(self, s: str) -> int:
            N = len(s)
            res = length = 1
            
            def f(x):
                return ord(x) - ord("a")
            
            prev = f(s[0])
            
            for i in range(1, N):
                curr = f(s[i])
                
                if prev + 1 == curr:
                    length += 1
                    res = max(res, length)
                else:
                    length = 1
                
                prev = curr
            
            return res
    
    
  • 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;
        }
    };
    
  • 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
    }
    
    func max(a, b int) int {
    	if a > b {
    		return a
    	}
    	return b
    }
    
  • 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);
    }
    
    
  • 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
        }
    }
    
    

Explain:

nope.

Complexity:

  • Time complexity : O(n).
  • Space complexity : O(n).

All Problems

All Solutions