Code

def getLongestSubstring(s: str) -> int:
    n = len(s)
    max_len = 0    # Iterate through each character in the string    for i in range(n - 1):
        # Try to expand the substring starting at s[i]        for j in range(i + 1, n):
            # If s[i] is lexicographically smaller than s[j], it's a valid substring            if s[i] < s[j]:
                # Update the maximum length found                max_len = max(max_len, j - i + 1)
    return max_len
# Example usages = "ecbdca"print(getLongestSubstring(s))  # Expected output: 3

OR

def getLongestSubstring(s: str) -> int:
    n = len(s)
    max_len = 0    # Iterate over all possible substrings    for i in range(n - 1):
        for j in range(i + 1, n):
            # Check if the first character is smaller than the last one            if s[i] < s[j]:
                max_len = max(max_len, j - i + 1)
    return max_len if max_len > 1 else 0