📌  相关文章
📜  给定字符串中非等距字符的三元组计数

📅  最后修改于: 2021-04-29 17:13:03             🧑  作者: Mango

给定一个包含字母“ a”,“ b”和“ c”的字符串s ,任务是计算由三个字符组成的所有可能的三胞胎,并且它们之间的距离不应相同。

例子:

方法:
要解决上述问题,我们需要执行以下步骤:

  • 计算给定字符串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 )