Number of subsequences as “ab” in a string repeated K times
Last Updated :
23 Mar, 2023
Given a String S, consider a new string formed by repeating the S exactly K times. We need find the number of subsequences as “ab” in the newly formed string.
Examples :
Input : S = "abcb"
K = 2
Output : 6
Here, Given string is repeated 2 times and
we get a new string "abcbabcb"
Below are 6 occurrences of "ab"
abcbabcb
abcbabcb
abcbabcb
abcbabcb
abcbabcb
abcbabcb
Input : S = "aacbd"
K = 1
Output : 2
Naive Approach: Finding no.of subsequences of “ab” is in fact finding a pair s[i], s[j] (i < j) where s[i] = ‘a’, s[j] = ‘b’.
We can do this by using two nested for loops and count the no. of pairs.
We can improve this approach in a single traversal of the string. Let us consider an index j, s[j] =’b’, if we find no.of index i‘s such that s[i] = ‘a’ and i < j, then it is same as no.of subsequences of “ab” till j. This can be done by maintaining count of a’s by traversing the array and add the count to our answer at position where s[i] =’b .
Implementation:-
C++
#include <bits/stdc++.h>
using namespace std;
int countOccurrences(string s, int K)
{
int ans=0;
int c=0;
string temp= "" ;
for ( int i=0;i<K;i++)
{
temp+=s;
}
for ( int i=0;i<temp.length();i++)
{
if (temp[i]== 'a' )c++;
else if (temp[i]== 'b' )ans+=c;
}
return ans;
}
int main()
{
string S = "abcb" ;
int k = 2;
cout << countOccurrences(S, k) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG {
public static int countOccurrences(String s, int K)
{
int ans = 0 ;
int c = 0 ;
String temp = "" ;
for ( int i = 0 ;i < K;i++)
{
temp += s;
}
for ( int i = 0 ;i < temp.length();i++)
{
if (temp.charAt(i) == 'a' )
{
c++;
}
else if (temp.charAt(i) == 'b' )
{
ans += c;
}
}
return ans;
}
public static void main(String[] args)
{
String S = "abcb" ;
int k = 2 ;
System.out.print(countOccurrences(S, k));
System.out.print( "\n" );
}
}
|
Python3
def countOccurrences(s, K):
ans = 0
c = 0
temp = ""
for i in range (K):
temp + = s
for i in range ( len (temp)):
if temp[i] = = 'a' :
c + = 1
elif temp[i] = = 'b' :
ans + = c
return ans
S = "abcb"
k = 2
print (countOccurrences(S, k))
|
C#
using System;
class MainClass {
static int countOccurrences( string s, int K) {
int ans = 0;
int c = 0;
string temp = "" ;
for ( int i = 0; i < K; i++) {
temp += s;
}
for ( int i = 0; i < temp.Length; i++) {
if (temp[i] == 'a' ) {
c += 1;
}
else if (temp[i] == 'b' ) {
ans += c;
}
}
return ans;
}
public static void Main ( string [] args) {
string S = "abcb" ;
int k = 2;
Console.WriteLine(countOccurrences(S, k));
}
}
|
Javascript
function countOccurrences(s, K) {
let ans = 0;
let c = 0;
let temp = "" ;
for (let i = 0; i < K; i++) {
temp += s;
}
for (let i = 0; i < temp.length; i++) {
if (temp[i] == 'a' ) {
c++;
}
else if (temp[i] == 'b' ) {
ans += c;
}
}
return ans;
}
const S = "abcb" ;
const k = 2;
console.log(countOccurrences(S, k));
|
Time Complexity:O(|S|*K)
Auxiliary Space :- O(|S|*K)
Efficient Approach:
Let T be the newly formed string
T = s1 + s2 + s3 + ….. + sk;
where si is the ith occurrence of the string s.
Here, occurrence of “ab” in T are as follows:
1)”ab” lies completely in the some of occurrence of string S, so we can simply find occurrences of “ab” in Si.Let it be C. So, total no.of occurrences of “ab” in T will be C*K.
2) Otherwise, ”a” lies strictly inside some string Si and “b” lies inside some other string Sj, (i < j). In this way finding no.of occurrences of “ab” will be choosing two occurrences of string S from K occurrences(KC2) and multiplying it with no. of occurrences of “a” in Si and no.of occurrences of “b” in Sj.
As, Si = Sj = S.
Time Complexity: O(|S|), for counting no.of “a”s and no.of “b”s.
C++
#include <bits/stdc++.h>
using namespace std;
int countOccurrences(string s, int K)
{
int n = s.length();
int C, c1 = 0, c2 = 0;
for ( int i = 0; i < n; i++) {
if (s[i] == 'a' )
c1++;
if (s[i] == 'b' ) {
c2++;
C += c1;
}
}
return C * K + (K * (K - 1) / 2) * c1 * c2;
}
int main()
{
string S = "abcb" ;
int k = 2;
cout << countOccurrences(S, k) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG {
static int countOccurrences(String s, int K)
{
int n = s.length();
int C = 0 , c1 = 0 , c2 = 0 ;
for ( int i = 0 ; i < n; i++) {
if (s.charAt(i) == 'a' )
c1++;
if (s.charAt(i) == 'b' ) {
c2++;
C += c1;
}
}
return C * K + (K * (K - 1 ) / 2 ) * c1 * c2;
}
public static void main(String[] args)
{
String S = "abcb" ;
int k = 2 ;
System.out.println(countOccurrences(S, k));
}
}
|
Python3
def countOccurrences (s, K):
n = len (s)
c1 = 0
c2 = 0
C = 0
for i in range (n):
if s[i] = = 'a' :
c1 + = 1
if s[i] = = 'b' :
c2 + = 1
C + = c1
return C * K + (K * (K - 1 ) / 2 ) * c1 * c2
S = "abcb"
k = 2
print (countOccurrences(S, k))
|
C#
using System;
class GFG {
static int countOccurrences( string s, int K)
{
int n = s.Length;
int C = 0, c1 = 0, c2 = 0;
for ( int i = 0; i < n; i++) {
if (s[i] == 'a' )
c1++;
if (s[i] == 'b' ) {
c2++;
C += c1;
}
}
return C * K + (K * (K - 1) / 2) * c1 * c2;
}
public static void Main()
{
string S = "abcb" ;
int k = 2;
Console.WriteLine(
countOccurrences(S, k));
}
}
|
PHP
<?php
function countOccurrences( $s , $K )
{
$n = strlen ( $s );
$C = 0; $c1 = 0; $c2 = 0;
for ( $i = 0; $i < $n ; $i ++)
{
if ( $s [ $i ] == 'a' )
$c1 ++;
if ( $s [ $i ] == 'b' )
{
$c2 ++;
$C = $C + $c1 ;
}
}
return $C * $K + ( $K * ( $K - 1) / 2) *
$c1 * $c2 ;
}
$S = "abcb" ;
$k = 2;
echo countOccurrences( $S , $k ), "\n" ;
?>
|
Javascript
<script>
function countOccurrences(s, K)
{
let n = s.length;
let C = 0, c1 = 0, c2 = 0;
for (let i = 0; i < n; i++)
{
if (s[i] == 'a' )
c1++;
if (s[i] == 'b' )
{
c2++;
C += c1;
}
}
return C * K + (K * (K - 1) / 2) * c1 * c2;
}
let S = "abcb" ;
let k = 2;
document.write(countOccurrences(S, k));
</script>
|
Time complexity:O(|S|).
Auxiliary Space: O(|S|)), where n is the length of the string. This is because when string is passed to any function it is passed by value and creates a copy of itself in stack.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...