Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to find length of longest consecutively increasing substring in Python
Given a lowercase string containing English letters and "?" symbols, we need to find the length of the longest consecutively increasing substring that starts with letter "a". For each "?" we can either remove it or replace it with any lowercase letter.
For example, if the input is s = "vta???defke", we can transform it into "vtabcdefke" where "abcdef" is the longest consecutively increasing substring starting with "a", giving us a length of 6.
Algorithm
We'll use a greedy approach to track the current sequence length and question marks ?
- maxlen − Maximum length found so far
- length − Current sequence length
- qmarks − Count of consecutive question marks
For each character, if it's a "?", we increment the question mark counter. If it's a letter, we check if it can extend our current sequence or start a new one.
Example
def solve(s):
maxlen = length = qmarks = 0
for c in s:
if c == "?":
qmarks += 1
else:
idx = ord(c) - ord("a")
# Check if character can extend current sequence
if length <= idx <= length + qmarks or idx <= qmarks:
length = idx + 1
else:
length = 0
qmarks = 0
# Update maximum length (max 26 letters in alphabet)
maxlen = max(maxlen, min(length + qmarks, 26))
return maxlen
# Test the function
s = "vta???defke"
result = solve(s)
print(f"Input: {s}")
print(f"Longest increasing substring length: {result}")
Input: vta???defke Longest increasing substring length: 6
How It Works
Let's trace through the example "vta???defke" ?
def solve_with_trace(s):
maxlen = length = qmarks = 0
print(f"Processing string: '{s}'")
print("Char | idx | length | qmarks | maxlen")
print("-" * 40)
for i, c in enumerate(s):
if c == "?":
qmarks += 1
else:
idx = ord(c) - ord("a")
if length <= idx <= length + qmarks or idx <= qmarks:
length = idx + 1
else:
length = 0
qmarks = 0
maxlen = max(maxlen, min(length + qmarks, 26))
print(f" {c} | {ord(c) - ord('a') if c != '?' else '?'} | {length} | {qmarks} | {maxlen}")
return maxlen
# Trace example
solve_with_trace("vta???defke")
Processing string: 'vta???defke' Char | idx | length | qmarks | maxlen ---------------------------------------- v | 21 | 0 | 0 | 0 t | 19 | 0 | 0 | 0 a | 0 | 1 | 0 | 1 ? | ? | 1 | 1 | 2 ? | ? | 1 | 2 | 3 ? | ? | 1 | 3 | 4 d | 3 | 4 | 0 | 4 e | 4 | 5 | 0 | 5 f | 5 | 6 | 0 | 6 k | 10 | 0 | 0 | 6 e | 4 | 0 | 0 | 6
Key Points
- The algorithm processes each character once, giving O(n) time complexity
- Question marks can be replaced optimally to extend sequences
- Maximum possible length is 26 (entire alphabet)
- The sequence must start with 'a' for optimal results
Conclusion
This greedy approach efficiently finds the longest consecutively increasing substring by tracking current sequence length and utilizing question marks optimally. The algorithm runs in linear time and handles the constraint of starting with 'a'.
