Count subsequence of length three in a given string
Given a string of length n and a subsequence of length 3. Find the total number of occurrences of the subsequence in this string.
Examples :
Input : string = "GFGFGYSYIOIWIN",
subsequence = "GFG"
Output : 4
Explanation : There are 4 such subsequences as shown:
GFGFGYSYIOIWIN
GFGFGYSYIOIWIN
GFGFGYSYIOIWIN
GFGFGYSYIOIWIN
Input : string = "GFGGGZZYNOIWIN",
subsequence = "GFG"
Output : 3
Brute Force Approach: The simplest way to solve this problem is to run three loops for the tree characters of the given subsequences. That starts a loop to find the first character of the subsequence in the string, once this character is found start a loop after this index to find the second character, once this character is found start a loop after this index to find the third character. Maintain a variable to store the number of occurrences of third character, i.e. number of occurrences of the subsequence.
Below is the implementation of above approach:
C++
#include<iostream>
using namespace std;
int findOccurrences(string str, string substr)
{
int counter = 0;
for ( int i=0;i<str.length();i++)
{
if (str[i]==substr[0])
{
for ( int j=i+1;j<str.length();j++)
{
if (str[j]==substr[1])
{
for ( int k = j+1; k<str.length(); k++)
{
if (str[k]==substr[2])
counter++;
}
}
}
}
}
return counter;
}
int main()
{
string str = "GFGFGYSYIOIWIN" ;
string substr = "GFG" ;
cout << findOccurrences(str, substr);
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
public class GfG{
public static int findOccurrences(String str1, String substr1)
{
int counter = 0 ;
char [] str = str1.toCharArray();
char [] substr = substr1.toCharArray();
for ( int i= 0 ; i < str1.length(); i++)
{
if (str[i] == substr[ 0 ])
{
for ( int j = i+ 1 ; j < str1.length(); j++)
{
if (str[j] == substr[ 1 ])
{
for ( int k = j+ 1 ; k < str1.length(); k++)
{
if (str[k] == substr[ 2 ])
counter++;
}
}
}
}
}
return counter;
}
public static void main(String argc[]){
String str = "GFGFGYSYIOIWIN" ;
String substr = "GFG" ;
System.out.println(findOccurrences(str, substr));
}
}
|
Python3
def findOccurrences( str , substr) :
counter = 0
for i in range ( 0 , len ( str )) :
if ( str [i] = = substr[ 0 ]) :
for j in range (i + 1 ,
len ( str )) :
if ( str [j] = = substr[ 1 ]) :
for k in range (j + 1 ,
len ( str )) :
if ( str [k] = = substr[ 2 ]) :
counter = counter + 1
return counter
str = "GFGFGYSYIOIWIN"
substr = "GFG"
print (findOccurrences( str , substr))
|
C#
using System;
public class GfG {
public static int findOccurrences( string str1,
string substr1)
{
int counter = 0;
for ( int i=0; i < str1.Length; i++)
{
if (str1[i] == substr1[0])
{
for ( int j = i+1; j < str1.Length; j++)
{
if (str1[j] == substr1[1])
{
for ( int k = j+1; k < str1.Length; k++)
{
if (str1[k] == substr1[2])
counter++;
}
}
}
}
}
return counter;
}
public static void Main()
{
string str1 = "GFGFGYSYIOIWIN" ;
string substr1 = "GFG" ;
Console.WriteLine(findOccurrences(str1, substr1));
}
}
|
PHP
<?php
function findOccurrences( $str , $substr )
{
$counter = 0;
for ( $i = 0; $i < strlen ( $str ); $i ++)
{
if ( $str [ $i ] == $substr [0])
{
for ( $j = $i + 1; $j < strlen ( $str ); $j ++)
{
if ( $str [ $j ] == $substr [1])
{
for ( $k = $j + 1; $k < strlen ( $str ); $k ++)
{
if ( $str [ $k ] == $substr [2])
$counter ++;
}
}
}
}
}
return $counter ;
}
$str = "GFGFGYSYIOIWIN" ;
$substr = "GFG" ;
echo findOccurrences( $str , $substr );
?>
|
Javascript
<script>
function findOccurrences(str1, substr1)
{
let counter = 0;
for (let i = 0; i < str1.length; i++)
{
if (str1[i] == substr1[0])
{
for (let j = i + 1; j < str1.length; j++)
{
if (str1[j] == substr1[1])
{
for (let k = j + 1; k < str1.length; k++)
{
if (str1[k] == substr1[2])
counter++;
}
}
}
}
}
return counter;
}
let str1 = "GFGFGYSYIOIWIN" ;
let substr1 = "GFG" ;
document.write(findOccurrences(str1, substr1));
</script>
|
Output :
4
Time Complexity: O(n^3)
Auxiliary Space: O(1)
Efficient Approach: The efficient method to solve this problem is to use the concept of pre-calculation. The idea is for every occurrence of the middle character of subsequence in the string, pre-compute two values. That is, number of occurrences of first character before it and number of occurrences of third character after it and multiply these two values to generate total occurrences for each occurrence of middle character.
Below is the implementation of this idea:
C++
#include<iostream>
using namespace std;
int findOccurrences(string str, string substr)
{
int n = str.length();
int preLeft[n] = {0};
int preRight[n] = {0};
if (str[0]==substr[0])
preLeft[0]++;
for ( int i=1;i<n;i++)
{
if (str[i]==substr[0])
preLeft[i] = preLeft[i-1] + 1;
else
preLeft[i] = preLeft[i-1];
}
if (str[n-1]==substr[2])
preRight[n-1]++;
for ( int i=n-2;i>=0;i--)
{
if (str[i]==substr[2])
preRight[i] = preRight[i+1] + 1;
else
preRight[i] = preRight[i+1];
}
int counter = 0;
for ( int i=1; i<n-1;i++)
{
if (str[i]==str[1])
{
int total = preLeft[i-1]*preRight[i+1];
counter += total;
}
}
return counter;
}
int main()
{
string str = "GFGFGYSYIOIWIN" ;
string substr = "GFG" ;
cout << findOccurrences(str,substr);
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
public class GfG{
public static int findOccurrences(String str1, String substr1)
{
int n = str1.length();
char [] str = str1.toCharArray();
char [] substr = substr1.toCharArray();
int [] preLeft = new int [n];
int [] preRight = new int [n];
if (str[ 0 ] == substr[ 0 ])
preLeft[ 0 ]++;
for ( int i = 1 ; i < n; i++)
{
if (str[i] == substr[ 0 ])
preLeft[i] = preLeft[i- 1 ] + 1 ;
else
preLeft[i] = preLeft[i- 1 ];
}
if (str[n- 1 ] == substr[ 2 ])
preRight[n- 1 ]++;
for ( int i = n- 2 ; i >= 0 ; i--)
{
if (str[i] == substr[ 2 ])
preRight[i] = preRight[i+ 1 ] + 1 ;
else
preRight[i] = preRight[i+ 1 ];
}
int counter = 0 ;
for ( int i = 1 ; i < n- 1 ; i++)
{
if (str[i] == str[ 1 ])
{
int total = preLeft[i- 1 ] * preRight[i+ 1 ];
counter += total;
}
}
return counter;
}
public static void main(String argc[]){
String str = "GFGFGYSYIOIWIN" ;
String substr = "GFG" ;
System.out.println(findOccurrences(str, substr));
}
}
|
Python3
def findOccurrences(str1, substr):
n = len (str1)
preLeft = [ 0 for i in range (n)]
preRight = [ 0 for i in range (n)]
if (str1[ 0 ] = = substr[ 0 ]):
preLeft[ 0 ] + = 1
for i in range ( 1 , n):
if (str1[i] = = substr[ 0 ]):
preLeft[i] = preLeft[i - 1 ] + 1
else :
preLeft[i] = preLeft[i - 1 ]
if (str1[n - 1 ] = = substr[ 2 ]):
preRight[n - 1 ] + = 1
i = n - 2
while (i > = 0 ):
if (str1[i] = = substr[ 2 ]):
preRight[i] = preRight[i + 1 ] + 1
else :
preRight[i] = preRight[i + 1 ]
i - = 1
counter = 0
for i in range ( 1 , n - 1 ):
if (str1[i] = = str1[ 1 ]):
total = preLeft[i - 1 ] * preRight[i + 1 ]
counter + = total
return counter
if __name__ = = '__main__' :
str1 = "GFGFGYSYIOIWIN"
substr = "GFG"
print (findOccurrences(str1, substr))
|
C#
using System;
public class GfG {
public static int findOccurrences( string str1,
string substr1)
{
int n = str1.Length;
int [] preLeft = new int [n];
int [] preRight = new int [n];
if (str1[0] == substr1[0])
preLeft[0]++;
for ( int i = 1; i < n; i++)
{
if (str1[i] == substr1[0])
preLeft[i] = preLeft[i-1] + 1;
else
preLeft[i] = preLeft[i-1];
}
if (str1[n-1] == substr1[2])
preRight[n-1]++;
for ( int i = n-2; i >= 0; i--)
{
if (str1[i] == substr1[2])
preRight[i] = preRight[i+1] + 1;
else
preRight[i] = preRight[i+1];
}
int counter = 0;
for ( int i = 1; i < n-1; i++)
{
if (str1[i] == str1[1])
{
int total = preLeft[i-1] * preRight[i+1];
counter += total;
}
}
return counter;
}
public static void Main()
{
string str1 = "GFGFGYSYIOIWIN" ;
string substr1 = "GFG" ;
Console.WriteLine(findOccurrences(str1, substr1));
}
}
|
PHP
<?php
function findOccurrences( $str ,
$substr )
{
$n = strlen ( $str );
$preLeft = array (0);
$preRight = array (0);
if ( $str [0] == $substr [0])
$preLeft [0]++;
for ( $i = 1; $i < $n ; $i ++)
{
if ( $str [ $i ] == $substr [0])
$preLeft [ $i ] = $preLeft [ $i - 1] + 1;
else
$preLeft [ $i ] = $preLeft [ $i - 1];
}
if ( $str [ $n - 1] == $substr [2])
$preRight [ $n - 1]++;
for ( $i = $n - 2; $i >= 0; $i --)
{
if ( $str [ $i ] == $substr [2])
$preRight [ $i ] = ( $preRight [ $i + 1] + 1);
else
$preRight [ $i ] = $preRight [ $i + 1];
}
$counter = 0;
for ( $i = 1; $i < $n - 1; $i ++)
{
if ( $str [ $i ] == $str [1])
{
$total = $preLeft [ $i - 1] *
$preRight [ $i + 1];
$counter += $total ;
}
}
return $counter ;
}
$str = "GFGFGYSYIOIWIN" ;
$substr = "GFG" ;
echo findOccurrences( $str , $substr );
?>
|
Javascript
<script>
function findOccurrences(str, substr)
{
let n = str.length;
let preLeft = new Array(n);
preLeft.fill(0);
let preRight = new Array(n);
preRight.fill(0);
if (str[0] == substr[0])
preLeft[0]++;
for (let i = 1; i < n; i++)
{
if (str[i] == substr[0])
preLeft[i] = preLeft[i-1] + 1;
else
preLeft[i] = preLeft[i-1];
}
if (str[n-1] == substr[2])
preRight[n-1]++;
for (let i = n-2; i >= 0; i--)
{
if (str[i] == substr[2])
preRight[i] = preRight[i+1] + 1;
else
preRight[i] = preRight[i+1];
}
let counter = 0;
for (let i = 1; i < n-1; i++)
{
if (str[i] == str[1])
{
let total = preLeft[i-1] * preRight[i+1];
counter += total;
}
}
return counter;
}
let str = "GFGFGYSYIOIWIN" ;
let substr = "GFG" ;
document.write(findOccurrences(str, substr));
</script>
|
Output:
4
Time Complexity: O(n)
Auxiliary Space: O(n)
Last Updated :
28 Jul, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...