计算给定数字字符串串联K次生成的字符串中的子序列01
给定一个字符串S和一个正整数K ,任务是找到由给定数字字符串S K 次串联生成的字符串中的子序列“01”的数量。
例子:
Input: S = “0171”, K = 2
Output: 6
Explanation:
The string formed by concatenation of S, K number of times is “01710171”. There are total 6 possible subsequences which are marked as bold = {“01710171″, “01710171″, “01710171″, “01710171“, “01710171″, “01710171“}.
Input: S = “230013110087”, K = 2
Output: 24
朴素方法:解决给定问题的最简单方法是通过连接S 、 K次生成结果字符串,然后从字符串中找到所有可能的对 (i, j) 使得(i < j)和S[i ] = 0和S[j] = 1 。
时间复杂度: O((N*K) 2 )
辅助空间: O(N*K)
高效方法:还可以通过观察以下 2 个案例来优化任务:
- 情况 1:子字符串“01”严格在P中每个出现的S内。假设C是S中“01”的出现次数,那么在P中它将是C*K 。
- 案例2:当' 0 '在第i次出现的S中,' 1 '在第j次出现的地方形成一个子序列“ 01 ”,使得i < j,然后找到“ 01 ”的出现次数将是与选择由((K)*(K – 1))/2给出的P中的两个字符串或字符串的出现相同。设该值为 S i和 S j并将其乘以 S i中“0”的出现次数(用cnt0表示)和 S j中“1”出现的次数(用cnt1表示)得到“01”的子序列。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to calculate the number of
// subsequences of "01"
int countSubsequence(string S, int N,
int K)
{
// Store count of 0's and 1's
int C = 0, C1 = 0, C0 = 0;
for (int i = 0; i < N; i++) {
if (S[i] == '1')
C1++;
else if (S[i] == '0')
C0++;
}
// Count of subsequences without
// concatenation
int B1 = 0;
for (int i = 0; i < N; i++) {
if (S[i] == '1')
B1++;
else if (S[i] == '0')
C = C + (C1 - B1);
}
// Case 1
int ans = C * K;
// Case 2
ans += (C1 * C0 * (((K) * (K - 1)) / 2));
// Return the total count
return ans;
}
// Driver Code
int main()
{
string S = "230013110087";
int K = 2;
int N = S.length();
cout << countSubsequence(S, N, K);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
class GFG {
// Function to calculate the number of
// subsequences of "01"
static int countSubsequence(String S, int N, int K)
{
// Store count of 0's and 1's
int C = 0, C1 = 0, C0 = 0;
for (int i = 0; i < N; i++) {
if (S.charAt(i) == '1')
C1++;
else if (S.charAt(i) == '0')
C0++;
}
// Count of subsequences without
// concatenation
int B1 = 0;
for (int i = 0; i < N; i++) {
if (S.charAt(i) == '1')
B1++;
else if (S.charAt(i) == '0')
C = C + (C1 - B1);
}
// Case 1
int ans = C * K;
// Case 2
ans += (C1 * C0 * (((K) * (K - 1)) / 2));
// Return the total count
return ans;
}
// Driver Code
public static void main(String[] args)
{
String S = "230013110087";
int K = 2;
int N = S.length();
System.out.println(countSubsequence(S, N, K));
}
}
// This code is contributed by Potta Lokesh
Python3
# python program for the above approach
# Function to calculate the number of
# subsequences of "01"
def countSubsequence(S, N, K):
# Store count of 0's and 1's
C = 0
C1 = 0
C0 = 0
for i in range(0, N):
if (S[i] == '1'):
C1 += 1
elif (S[i] == '0'):
C0 += 1
# Count of subsequences without
# concatenation
B1 = 0
for i in range(0, N):
if (S[i] == '1'):
B1 += 1
elif (S[i] == '0'):
C = C + (C1 - B1)
# Case 1
ans = C * K
# Case 2
ans += (C1 * C0 * (((K) * (K - 1)) // 2))
# Return the total count
return ans
# Driver Code
if __name__ == "__main__":
S = "230013110087"
K = 2
N = len(S)
print(countSubsequence(S, N, K))
# This code is contributed by rakeshsahni
C#
// C# implementation for the above approach
using System;
class GFG
{
// Function to calculate the number of
// subsequences of "01"
static int countSubsequence(string S, int N, int K)
{
// Store count of 0's and 1's
int C = 0, C1 = 0, C0 = 0;
for (int i = 0; i < N; i++) {
if (S[i] == '1')
C1++;
else if (S[i] == '0')
C0++;
}
// Count of subsequences without
// concatenation
int B1 = 0;
for (int i = 0; i < N; i++) {
if (S[i] == '1')
B1++;
else if (S[i] == '0')
C = C + (C1 - B1);
}
// Case 1
int ans = C * K;
// Case 2
ans += (C1 * C0 * (((K) * (K - 1)) / 2));
// Return the total count
return ans;
}
// Driver Code
public static void Main()
{
string S = "230013110087";
int K = 2;
int N = S.Length;
Console.Write(countSubsequence(S, N, K));
}
}
// This code is contributed by sanjoy_62.
Javascript
输出:
24
时间复杂度: O(N)
辅助空间: O(1)