Length of the Longest Alphabetical Continuous Substring(Leetcode 2414)

Problem Link: https://leetcode.com/contest/weekly-contest-311/problems/length-of-the-longest-alphabetical-continuous-substring/

class Solution:
    def longestContinuousSubstring(self, s: str) -> int:
        
        n=len(s)
        j=0
        c=0
        ans=0
        for i in range(n):
            if(ord(s[i])-ord(s[j])==i-j):
                continue
            else:
                ans=max(ans,i-j)
                j=i
        
        # If j>=n, check the below condition
        ans=max(ans,i-j+1)
        return ans

Last updated