- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions

# Program to find length of longest consecutively increasing substring in Python

Suppose we have a lowercase string s. This contains English letters as well as "?" Symbol. For each "?" we must either remove it or replace it with any lowercase letter. We have to find the length of the longest consecutively increasing substring that starts with letter "a".

So, if the input is like s = "vta???defke", then the output will be 6, as we can turn s into "vtabcdefke" and "abcdef" is the longest consecutively increasing substring, and this is also starting with "a".

To solve this, we will follow these steps −

- maxlen := 0
- length := 0
- qmarks := 0
- for each c in s, do
- if c is same as "?", then
- qmarks := qmarks + 1

- otherwise,
- idx := (ASCII of c) - (ASCII of "a")
- length := idx + 1 if length <= idx <= length + qmarks or idx <= qmarks otherwise 0
- qmarks := 0

- maxlen := maximum of maxlen and (minimum of length + qmarks and 26)

- if c is same as "?", then
- return maxlen

## Example

Let us see the following implementation to get better understanding −

def solve(s): maxlen = length = qmarks = 0 for c in s: if c == "?": qmarks += 1 else: idx = ord(c) - ord("a") length = idx + 1 if length <= idx <= length + qmarks or idx <= qmarks else 0 qmarks = 0 maxlen = max(maxlen, min(length + qmarks, 26)) return maxlen s = "vta???defke" print(solve(s))

## Input

"vta???defke"

## Output

6

- Related Articles
- Program to find length of longest increasing subsequence in Python
- Program to find length of longest palindromic substring in Python
- Program to find length of longest circular increasing subsequence in python
- Program to find length of longest repeating substring in a string in Python
- Program to find length of longest common substring in C++
- Program to find length of longest strictly increasing then decreasing sublist in Python
- Program to find length of longest palindromic substring after single rotation in Python
- Program to find length of longest substring with even vowel counts in Python
- Program to find length of longest contiguously strictly increasing sublist after removal in Python
- Program to find length of longest substring which contains k distinct characters in Python
- Program to find longest awesome substring in Python
- Program to find the length of longest substring which has two distinct elements in Python
- Program to find length of longest substring with character count of at least k in Python
- Program to length of longest increasing path in a given matrix in Python
- Program to find longest nice substring using Python

Advertisements