Find number of Strictly Increasing Sequences with ‘a’ and ‘b’ in a String
Last Updated :
06 Feb, 2024
Given a string S of length N (1 <= N <= 105) consisting of lower case alphabets. The task is to find the number of strictly increasing sequences P1,P2, …, Pk such that:
- For each i (1 ≤ i ≤ k), Spi = ‘a’
- For each i (1 ≤ i < k), there exists j such that pi < j < pi+1 and Sj = ‘b’
The result should be calculated modulo 10^9 + 7.
Example:
Input: s = “abbaa”
Output: 5
Input: s = “baaaa”
Output: 4
Approach:
The idea is to use a greedy approach to solve this problem. Maintains two variables, result and temp, where result stores the current number of strictly increasing sequences ending with ‘a’ and temp stores the number of strictly increasing sequences ending with ‘a’ before the last ‘b’ was encountered.
Iterates over each character in the string
- Check if the character is ‘a’, then increments result by temp + 1. The +1 is for the sequence ending at the current ‘a’, and temp adds the sequences that can be formed by appending the current ‘a’ to all sequences ending with ‘a’ before the last ‘b’.
- If the character is ‘b’, it updates temp to result, preparing for the next ‘a’ characters.
Finally, the result is the total number of strictly increasing sequences ending with ‘a’.
Steps-by-step apprach:
- Set result and temp to 0. These variables will be used to keep track of the final result and a temporary value.
- Loop through each character in the given string (s).
- Check for ‘a’: Inside the loop, if the current character is ‘a’, update the result using the formula (result + temp + 1) % 1000000007.
- Check for ‘b’: If the current character is ‘b’, update the temp value using the result.
- After iterating through the string, return the final result
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int solve(string & s)
{
int result = 0, temp = 0;
for ( int i = 0; i < s.size(); i++) {
if (s[i] == 'a' )
result = (result + temp + 1) % 1000000007;
if (s[i] == 'b' )
temp = result;
}
return result;
}
int main()
{
string s = "abbaa" ;
cout << solve(s);
return 0;
}
|
Java
public class LexicographicallyLargest {
public static int solve(String s)
{
int result = 0 , temp = 0 ;
for ( int i = 0 ; i < s.length(); i++) {
if (s.charAt(i) == 'a' )
result = (result + temp + 1 ) % 1000000007 ;
if (s.charAt(i) == 'b' )
temp = result;
}
return result;
}
public static void main(String[] args)
{
String s = "abbaa" ;
System.out.println(solve(s));
}
}
|
Python3
def solve(s):
result = 0
temp = 0
for char in s:
if char = = 'a' :
result = (result + temp + 1 ) % 1000000007
if char = = 'b' :
temp = result
return result
s = "abbaa"
print (solve(s))
|
C#
using System;
class Program {
static int Solve( string s)
{
int result = 0, temp = 0;
for ( int i = 0; i < s.Length; i++) {
if (s[i] == 'a' )
result = (result + temp + 1) % 1000000007;
if (s[i] == 'b' )
temp = result;
}
return result;
}
static void Main()
{
string s = "abbaa" ;
Console.WriteLine(Solve(s));
}
}
|
Javascript
function solve(s) {
let result = 0;
let temp = 0;
for (let i = 0; i < s.length; i++) {
if (s[i] === 'a' ) {
result = (result + temp + 1) % 1000000007;
}
if (s[i] === 'b' ) {
temp = result;
}
}
return result;
}
const s = "abbaa" ;
console.log(solve(s));
|
Time Complexity: O(N), where N is the length of the input string.
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...