给定一个包含字母’a’、’b’和’c’的字符串s ,任务是计算由三个字符组成的所有可能的三元组,并且它们之间的距离不应该相同。
Distance between two letters is basically the difference between their indices.
例子:
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
方法:
要解决上述问题,我们需要按照以下步骤操作:
- 计算给定字符串s 中字母 ‘a’、’b’、’c’ 的所有出现次数。
- 通过将它们各自的计数相乘,找出包含 a、b、c 的所有三元组。
- 在此之后减去索引之间具有相同距离的所有三元组并打印结果输出。
下面是上述方法的实现:
C++
// C++ Program to count of triplets
// from the given string with
// non-equidistant characters
#include
using namespace std;
// Function to count valid triplets
void CountValidTriplet(string s, int n)
{
// Store frequencies of a, b and c
int count_a = 0,
count_b = 0,
count_c = 0;
for (int i = 0; i < n; i++) {
// If the current letter is 'a'
if (s[i] == 'a')
count_a++;
// If the current letter is 'b'
if (s[i] == 'b')
count_b++;
// If the current letter is 'c'
if (s[i] == 'c')
count_c++;
}
// Calculate total no of triplets
int Total_triplet = count_a
* count_b
* count_c;
// Subtract invalid triplets
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;
}
// Driver Code
int main()
{
string s = "abcbcabc";
int n = s.length();
CountValidTriplet(s, n);
return 0;
}
Java
// Java program to count of triplets
// from the given string with
// non-equidistant characters
import java.util.*;
class GFG{
// Function to count valid triplets
static void CountValidTriplet(String s, int n)
{
// Store frequencies of a, b and c
int count_a = 0,
count_b = 0,
count_c = 0;
for(int i = 0; i < n; i++)
{
// If the current letter is 'a'
if (s.charAt(i) == 'a')
count_a++;
// If the current letter is 'b'
if (s.charAt(i) == 'b')
count_b++;
// If the current letter is 'c'
if (s.charAt(i) == 'c')
count_c++;
}
// Calculate total no of triplets
int Total_triplet = count_a * count_b * count_c;
// Subtract invalid triplets
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);
}
// Driver code
public static void main(String[] args)
{
String s = "abcbcabc";
int n = s.length();
CountValidTriplet(s, n);
}
}
// This code is contributed by offbeat
Python3
# Python3 program to count of triplets
# from the given string with
# non-equidistant characters
# Function to count valid triplets
def CountValidTriplet(s, n):
# Store frequencies of a, b and c
count_a = 0
count_b = 0
count_c = 0
for i in range(n):
# If the current letter is 'a'
if (s[i] == 'a'):
count_a += 1
# If the current letter is 'b'
if (s[i] == 'b'):
count_b += 1
# If the current letter is 'c'
if (s[i] == 'c'):
count_c += 1
# Calculate total no of triplets
Total_triplet = count_a * count_b * count_c
# Subtract invalid triplets
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)
# Driver Code
s = "abcbcabc"
n = len(s)
CountValidTriplet(s, n)
# This code is contributed by yatinagg
C#
// C# program to count of triplets
// from the given string with
// non-equidistant characters
using System;
class GFG{
// Function to count valid triplets
static void CountValidTriplet(string s, int n)
{
// Store frequencies of a, b and c
int count_a = 0,
count_b = 0,
count_c = 0;
for(int i = 0; i < n; i++)
{
// If the current letter is 'a'
if (s[i] == 'a')
count_a++;
// If the current letter is 'b'
if (s[i] == 'b')
count_b++;
// If the current letter is 'c'
if (s[i] == 'c')
count_c++;
}
// Calculate total no of triplets
int Total_triplet = count_a * count_b * count_c;
// Subtract invalid triplets
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);
}
// Driver code
public static void Main()
{
string s = "abcbcabc";
int n = s.Length;
CountValidTriplet(s, n);
}
}
// This code is contributed by Code_Mech
输出:
13
时间复杂度: O(N 2 )
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live