Count of triplets from the given string with non-equidistant characters
Last Updated :
06 Aug, 2021
Given a string s containing letters ‘a’, ‘b’ and ‘c’, the task is to calculate all possible triplets made up of the three characters, and the distance between them should not be the same.
Distance between two letters is basically the difference between their indices.
Examples:
Input: s = “aabc”
Output: 1
Explanation:
Two possible triplets comprising of ‘a’, ‘b’ and ‘c’ are {1, 3, 4} and { 2, 3, 4}.
But the characters in the second triplet are equidistant. Hence, there is only one possible triplet satisfying the above condition.
Input: s = “abcbcabc”
Output: 13
Approach:
To solve the problem mentioned above, we need to follow the steps given below:
- Count all the occurrences of letters ‘a’, ‘b’, ‘c’ in the given string s.
- Find all triplets containing a, b, c by multiplying their respective counts.
- After this subtract all those triplets which has same distance between indices and print the resultant output.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void CountValidTriplet(string s, int n)
{
int count_a = 0,
count_b = 0,
count_c = 0;
for ( int i = 0; i < n; i++) {
if (s[i] == 'a' )
count_a++;
if (s[i] == 'b' )
count_b++;
if (s[i] == 'c' )
count_c++;
}
int Total_triplet = count_a
* count_b
* count_c;
for ( int i = 0; i < n; i++) {
for ( int j = i + 1; j < n; j++) {
if ((2 * j - i) < n
&& s[j] != s[i]
&& s[j * 2 - i] != s[j]
&& s[2 * j - i] != s[i])
Total_triplet--;
}
}
cout << Total_triplet;
}
int main()
{
string s = "abcbcabc" ;
int n = s.length();
CountValidTriplet(s, n);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void CountValidTriplet(String s, int n)
{
int count_a = 0 ,
count_b = 0 ,
count_c = 0 ;
for ( int i = 0 ; i < n; i++)
{
if (s.charAt(i) == 'a' )
count_a++;
if (s.charAt(i) == 'b' )
count_b++;
if (s.charAt(i) == 'c' )
count_c++;
}
int Total_triplet = count_a * count_b * count_c;
for ( int i = 0 ; i < n; i++)
{
for ( int j = i + 1 ; j < n; j++)
{
if (( 2 * j - i) < n &&
s.charAt(j) != s.charAt(i) &&
s.charAt(j * 2 - i) != s.charAt(j) &&
s.charAt( 2 * j - i) != s.charAt(i))
Total_triplet--;
}
}
System.out.println(Total_triplet);
}
public static void main(String[] args)
{
String s = "abcbcabc" ;
int n = s.length();
CountValidTriplet(s, n);
}
}
|
Python3
def CountValidTriplet(s, n):
count_a = 0
count_b = 0
count_c = 0
for i in range (n):
if (s[i] = = 'a' ):
count_a + = 1
if (s[i] = = 'b' ):
count_b + = 1
if (s[i] = = 'c' ):
count_c + = 1
Total_triplet = count_a * count_b * count_c
for i in range (n):
for j in range (i + 1 , n):
if (( 2 * j - i) < n and
s[j] ! = s[i] and
s[j * 2 - i] ! = s[j] and
s[ 2 * j - i] ! = s[i]):
Total_triplet - = 1
print (Total_triplet)
s = "abcbcabc"
n = len (s)
CountValidTriplet(s, n)
|
C#
using System;
class GFG{
static void CountValidTriplet( string s, int n)
{
int count_a = 0,
count_b = 0,
count_c = 0;
for ( int i = 0; i < n; i++)
{
if (s[i] == 'a' )
count_a++;
if (s[i] == 'b' )
count_b++;
if (s[i] == 'c' )
count_c++;
}
int Total_triplet = count_a * count_b * count_c;
for ( int i = 0; i < n; i++)
{
for ( int j = i + 1; j < n; j++)
{
if ((2 * j - i) < n &&
s[j] != s[i] &&
s[j * 2 - i] != s[j] &&
s[2 * j - i] != s[i])
Total_triplet--;
}
}
Console.Write(Total_triplet);
}
public static void Main()
{
string s = "abcbcabc" ;
int n = s.Length;
CountValidTriplet(s, n);
}
}
|
Javascript
<script>
function CountValidTriplet(s, n)
{
let count_a = 0,
count_b = 0,
count_c = 0;
for (let i = 0; i < n; i++)
{
if (s[i] == 'a' )
count_a++;
if (s[i] == 'b' )
count_b++;
if (s[i] == 'c' )
count_c++;
}
let Total_triplet = count_a * count_b * count_c;
for (let i = 0; i < n; i++)
{
for (let j = i + 1; j < n; j++)
{
if ((2 * j - i) < n &&
s[j] != s[i] &&
s[j * 2 - i] != s[j] &&
s[2 * j - i] != s[i])
Total_triplet--;
}
}
document.write(Total_triplet);
}
let s = "abcbcabc" ;
let n = s.length;
CountValidTriplet(s, n);
</script>
|
Time complexity: O(N2). where N is the length of the given string.
Auxiliary Space: O(1).
Share your thoughts in the comments
Please Login to comment...