索引对 (i, j) 的计数,使得删除第 i 个字符后的字符串等于删除第 j 个字符后的字符串
给定一个包含N个字符的字符串str ,任务是计算(i, j)的有效无序对的计数,使得删除第 i个字符后的字符串等于删除第j个字符后的字符串。
例子:
Input: str = “aabb”
Output: 2
Explanation: The string after deletion of 1st element is “abb” and the string after deletion of 2nd element is “abb”. Similarly, the string after deletion of 3rd element is “aab” and the string after deletion of 4th element is “aab”. Hence, the number of valid pairs of (i, j) are 2, i.e, (1, 2) and (3, 4).
Input: str = “aaaaaa”
Output: 15
方法:给定的问题可以通过以下观察来解决:
- If Si = Sj, then Si = Si+1 = Si+2 … = Sj must hold true.
- Also if Si = Sj, then str[i] = str[i+1] = str[i+2] … = str[j] must hold true as well.
因此,使用上述观察,可以使用双指针技术计算字符串str中的间隔(l, r) ,使得str[l] = str[l+1] ... = str[r] ,并且对于每个有效的(l, r) ,有效对的计数将为r – l C 2 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the count of valid
// pairs (i, j) such that string after
// deleting ith character is equal to
// string after deleting jth character
int countValidPairs(string str, int N)
{
// Stores the required count
int ans = 0;
// Loop to iterate the given array
for (int l = 0, r; l < N; l = r) {
// initial end point
r = l;
// Loop to calculate the range
// [l, r] such that all characters
// from l to r are equal
while (r < N && str[l] == str[r]) {
r++;
}
// Update the answer
ans += ((r - l) * (r - l - 1)) / 2;
}
// Return Answer
return ans;
}
// Driver Code
int main()
{
string str = "aaaaaa";
cout << countValidPairs(str, str.length());
return 0;
}
Java
// Java program for the above approach
import java.util.*;
public class GFG
{
// Function to find the count of valid
// pairs (i, j) such that string after
// deleting ith character is equal to
// string after deleting jth character
static int countValidPairs(String str, int N)
{
// Stores the required count
int ans = 0;
// Loop to iterate the given array
for (int l = 0, r; l < N; l = r) {
// initial end point
r = l;
// Loop to calculate the range
// [l, r] such that all characters
// from l to r are equal
Character c1 = str.charAt(l);
Character c2 = str.charAt(r);
while (r < N && c1.equals(c2)) {
r++;
}
// Update the answer
ans += ((r - l) * (r - l - 1)) / 2;
}
// Return Answer
return ans;
}
// Driver Code
public static void main(String args[])
{
String str = "aaaaaa";
System.out.print(countValidPairs(str, str.length()));
}
}
// This code is contributed by Samim Hossain Mondal.
Python3
# Python program for the above approach
# Function to find the count of valid
# pairs (i, j) such that string after
# deleting ith character is equal to
# string after deleting jth character
def countValidPairs(str, N):
# Stores the required count
ans = 0;
# Loop to iterate the given array
l = 0;
r = None;
while(l < N):
# initial end point
r = l;
# Loop to calculate the range
# [l, r] such that all characters
# from l to r are equal
while (r < N and str[l] == str[r]):
r += 1
# Update the answer
ans += ((r - l) * (r - l - 1)) // 2;
l = r;
# Return Answer
return ans;
# Driver Code
str = "aaaaaa";
print(countValidPairs(str, len(str)));
# This code is contributed by Saurabh Jaiswal
C#
// C# program for the above approach
using System;
class GFG
{
// Function to find the count of valid
// pairs (i, j) such that string after
// deleting ith character is equal to
// string after deleting jth character
static int countValidPairs(string str, int N)
{
// Stores the required count
int ans = 0;
// Loop to iterate the given array
for (int l = 0, r; l < N; l = r) {
// initial end point
r = l;
// Loop to calculate the range
// [l, r] such that all characters
// from l to r are equal
char c1 = str[l];
char c2 = str[r];
while (r < N && c1.Equals(c2)) {
r++;
}
// Update the answer
ans += ((r - l) * (r - l - 1)) / 2;
}
// Return Answer
return ans;
}
// Driver Code
public static void Main()
{
string str = "aaaaaa";
Console.Write(countValidPairs(str, str.Length));
}
}
// This code is contributed by Samim Hossain Mondal.
Javascript
输出
15
时间复杂度: O(N)
辅助空间: O(1)