- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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

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))

"vta???defke"

6

- Related Questions & Answers
- 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 common substring in C++
- Program to find length of longest strictly increasing then decreasing sublist in Python
- Program to find length of longest repeating substring in a string 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 matrix path length 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

Advertisements