📌  相关文章
📜  给定原始字符串中有序三元组(R,G,B)的计数

📅  最后修改于: 2021-04-23 20:05:37             🧑  作者: Mango

给定大小为N的字符串S ,任务是对给定字符串中的所有可能的三胞胎进行计数,该字符串仅按(R,G,B)的顺序由3种颜色(R)红色,(G)绿色和(B)蓝色组成。

例子:

方法:

  1. 计算给定字符串中B(Blue)的数目,并将该值存储在Blue_Count变量中。
  2. 初始化Red_Count = 0。
  3. 从左到右遍历字符串的所有字符。
  4. 如果当前字符是(R)red,则增加Red_Count
  5. 如果当前字符为(B)blue,则减少Blue_Count
  6. 如果当前字符为G(绿色),则在结果中添加Red_Count * Blue_Count

下面是上述方法的实现。

C++
// C++ code for the above program
  
#include 
using namespace std;
  
// function to count the
// ordered triplets (R, G, B)
int countTriplets(string color)
{
    int result = 0, Blue_Count = 0;
    int Red_Count = 0;
  
    // count the B(blue) colour
    for (char c : color) {
        if (c == 'B')
            Blue_Count++;
    }
  
    for (char c : color) {
        if (c == 'B')
            Blue_Count--;
        if (c == 'R')
            Red_Count++;
        if (c == 'G')
            result += Red_Count * Blue_Count;
    }
    return result;
}
  
// Driver program
int main()
{
    string color = "RRGGBBRGGBB";
    cout << countTriplets(color);
    return 0;
}


Java
// Java code for the above program
class GFG{
          
// function to count the
// ordered triplets (R, G, B)
static int countTriplets(String color)
{
    int result = 0, Blue_Count = 0;
    int Red_Count = 0;
    int len = color.length();
    int i;
      
    // count the B(blue) colour
    for (i = 0; i < len ; i++)
    {
        if (color.charAt(i) == 'B')
            Blue_Count++;
    }
  
    for (i = 0; i < len ; i++)
    {
        if (color.charAt(i) == 'B')
            Blue_Count--;
        if (color.charAt(i) == 'R')
            Red_Count++;
        if (color.charAt(i) == 'G')
            result += Red_Count * Blue_Count;
    }
    return result;
}
  
// Driver Code
public static void main (String[] args)
{
    String color = "RRGGBBRGGBB";
    System.out.println(countTriplets(color));
}
  
}
  
// This code is contributed by AnkitRai01


Python3
# Python3 code for the above program 
  
# function to count the 
# ordered triplets (R, G, B) 
def countTriplets(color) :
  
    result = 0; Blue_Count = 0; 
    Red_Count = 0; 
  
    # count the B(blue) colour 
    for c in color :
        if (c == 'B') :
            Blue_Count += 1; 
  
    for c in color : 
        if (c == 'B') :
            Blue_Count -= 1; 
              
        if (c == 'R') :
            Red_Count += 1; 
              
        if (c == 'G') :
            result += Red_Count * Blue_Count; 
      
    return result; 
  
# Driver Code
if __name__ == "__main__" : 
  
    color = "RRGGBBRGGBB"; 
    print(countTriplets(color)); 
  
# This code is contributed by AnkitRai01


C#
// C# code for the above program
using System;
class GFG{
          
// function to count the
// ordered triplets (R, G, B)
static int countTriplets(String color)
{
    int result = 0, Blue_Count = 0;
    int Red_Count = 0;
    int len = color.Length;
    int i;
  
    // count the B(blue) colour
    for (i = 0; i < len ; i++)
    {
        if (color[i] == 'B')
            Blue_Count++;
    }
  
    for (i = 0; i < len ; i++)
    {
        if (color[i] == 'B')
            Blue_Count--;
        if (color[i] == 'R')
            Red_Count++;
        if (color[i] == 'G')
            result += Red_Count * Blue_Count;
    }
    return result;
}
  
// Driver Code
public static void Main(string[] args)
{
    string color = "RRGGBBRGGBB";
    Console.WriteLine(countTriplets(color));
}
}
  
// This code is contributed by AnkitRai01


输出:
28

时间复杂度: O(N)
辅助空间复杂度: O(1)