具有索引 (i, j, k) 的有序三元组计数代表不同的值和 j – i != k – j
给定一个仅由三个字符组成的三元字符串S ,任务是找到有序三元组(i, j, k)的计数,使得S[i] 、 S[j]和S[k]是不同的并且j – i != k - j 。
例子:
Input: str = “AABC”
Output: 1
Explanation: Only the triplet (0, 2, 3) satisfies the required conditions as S[0], S[2], and S[3] are distinct and 2 – 0 != 3 – 2. The triplet (1, 2, 3) also represents all distinct characters but 2 – 1 = 3 – 2 violating the 2nd condition.
Input: str = “PQRRPQQR”
Output: 13
方法:给定的问题可以通过将问题分成两部分来解决。这个想法是计算具有不同字符的三元组的计数,然后减去三元组,使它们具有不同的字符并且j - i = k - j 。以下是计算两者的步骤:
- 可以通过计算假设 X、Y、Z 三个字符的频率来计算具有不同字符的三元组的总数。因此,所有可能的对将是X C 1 * Y C 1 * Z C 1 => X * Y * Z ,即它们的频率的乘积。
- 可以看出,方程k – j = j – i可以转换为k = 2*j – i 。因此,遍历(i, j)的所有可能值并计算k的值。如果索引(i, j, k)表示不同的字符,则增加计数。
- 因此,所需答案将是上述步骤中计算的值的差异。
下面是上述方法的实现:
C++
// C++ Program of the above approach
#include
using namespace std;
// Function to find count of triplets
// with indices (i, j, k) representing
// distinct values and j - i != k - j
int cntTriplets(string S, int N)
{
// Stores the frequency of
// characters in string
unordered_map freq;
for (int i = 0; i < N; i++) {
freq[S[i]]++;
}
// Stores the product
// of frequencies
int prod = 1;
for (auto x : freq) {
prod *= x.second;
}
// If string has less than
// three distinct characters
if (freq.size() < 3) {
prod = 0;
}
// Stores the triplets
// with j-i = k-j
int cnt = 0;
// Loop to iterate over all
// ordered pairs of (i, j)
for (int i = 0; i < N; i++) {
for (int j = i + 1; j < N; j++) {
// Calculate K
int k = 2 * j - i;
// If k is out of bound
if (k >= N)
break;
// If S[i], S[j] and
// S[k] are unique
if (S[i] != S[j] && S[j] != S[k]
&& S[i] != S[k])
cnt++;
}
}
return prod - cnt;
}
// Driver Code
int main()
{
string S = "PQRRPQQR";
cout << cntTriplets(S, S.length());
return 0;
}
Java
// Java program for the given approach
import java.util.HashMap;
class GFG {
// Function to find count of triplets
// with indices (i, j, k) representing
// distinct values and j - i != k - j
static int cntTriplets(String S, int N) {
// Stores the frequency of
// characters in String
HashMap freq = new HashMap();
for (int i = 0; i < N; i++) {
if (freq.containsKey(S.charAt(i))) {
freq.put(S.charAt(i), freq.get(S.charAt(i)) + 1);
} else {
freq.put(S.charAt(i), 1);
}
}
// Stores the product
// of frequencies
int prod = 1;
for (char x : freq.keySet()) {
prod *= freq.get(x);
}
// If String has less than
// three distinct characters
if (freq.size() < 3) {
prod = 0;
}
// Stores the triplets
// with j-i = k-j
int cnt = 0;
// Loop to iterate over all
// ordered pairs of (i, j)
for (int i = 0; i < N; i++) {
for (int j = i + 1; j < N; j++) {
// Calculate K
int k = 2 * j - i;
// If k is out of bound
if (k >= N)
break;
// If S[i], S.charAt(j) and
// S.charAt(k) are unique
if (S.charAt(i) != S.charAt(j) && S.charAt(j) != S.charAt(k)
&& S.charAt(i) != S.charAt(k))
cnt++;
}
}
return prod - cnt;
}
// Driver code
public static void main(String args[]) {
String S = "PQRRPQQR";
System.out.println(cntTriplets(S, S.length()));
}
}
// This code is contributed by gfgking
Python3
# Python 3 Program of the above approach
from collections import defaultdict
# Function to find count of triplets
# with indices (i, j, k) representing
# distinct values and j - i != k - j
def cntTriplets(S, N):
# Stores the frequency of
# characters in string
freq = defaultdict(int)
for i in range(N):
freq[S[i]] += 1
# Stores the product
# of frequencies
prod = 1
for x in freq:
prod *= freq[x]
# If string has less than
# three distinct characters
if (len(freq) < 3):
prod = 0
# Stores the triplets
# with j-i = k-j
cnt = 0
# Loop to iterate over all
# ordered pairs of (i, j)
for i in range(N):
for j in range(i + 1, N):
# Calculate K
k = 2 * j - i
# If k is out of bound
if (k >= N):
break
# If S[i], S[j] and
# S[k] are unique
if (S[i] != S[j] and S[j] != S[k]
and S[i] != S[k]):
cnt += 1
return prod - cnt
# Driver Code
if __name__ == "__main__":
S = "PQRRPQQR"
print(cntTriplets(S, len(S)))
# This code is contributed by ukasp.
C#
// C# program for the given approach
using System;
using System.Collections.Generic;
class GFG
{
// Function to find count of triplets
// with indices (i, j, k) representing
// distinct values and j - i != k - j
static int cntTriplets(string S, int N)
{
// Stores the frequency of
// characters in string
Dictionary freq =
new Dictionary();
for (int i = 0; i < N; i++) {
if (freq.ContainsKey(S[i]))
{
freq[S[i]] = freq[S[i]] + 1;
}
else
{
freq.Add(S[i], 1);
}
}
// Stores the product
// of frequencies
int prod = 1;
foreach(KeyValuePair x in freq)
{
prod *= x.Value;
}
// If string has less than
// three distinct characters
if (freq.Count < 3) {
prod = 0;
}
// Stores the triplets
// with j-i = k-j
int cnt = 0;
// Loop to iterate over all
// ordered pairs of (i, j)
for (int i = 0; i < N; i++) {
for (int j = i + 1; j < N; j++) {
// Calculate K
int k = 2 * j - i;
// If k is out of bound
if (k >= N)
break;
// If S[i], S[j] and
// S[k] are unique
if (S[i] != S[j] && S[j] != S[k]
&& S[i] != S[k])
cnt++;
}
}
return prod - cnt;
}
// Driver code
public static void Main()
{
string S = "PQRRPQQR";
Console.Write(cntTriplets(S, S.Length));
}
}
// This code is contributed by Samim Hossain Mondal.
Javascript
输出
13
时间复杂度: O(N 2 )
辅助空间: O(1)