📌  相关文章
📜  重复K次的字符串作为“ ab”的子序列数

📅  最后修改于: 2021-05-04 16:42:57             🧑  作者: Mango

给定字符串S,请考虑通过将S精确地重复K次而形成的新字符串。我们需要在新形成的字符串找到子序列个数“ ab”。

例子 :

Input : S = "abcb"
        K = 2
Output : 6
Here, Given string is repeated 2 times and
we get a new string "abcbabcb"
Below are 6 occurrences of "ab"
abcbabcb
abcbabcb
abcbabcb
abcbabcb
abcbabcb
abcbabcb

Input : S = "aacbd"
        K = 1
Output : 2

天真的方法:找到“ ab”的子序列号实际上是找到一对s [i],s [j](i 我们可以通过使用两个嵌套的for循环并计数no来做到这一点。对。
我们可以在字符串的单个遍历中改进此方法。让我们考虑一个索引j, s [j] =’b’ ,如果我们发现索引i的数量等于s [i] =’a’且i 时间复杂度:O(| S | * K)

高效方法:
令T为新形成的字符串
T = s1 + s2 + s3 +….. + sk;
其中si是字符串s的第i个出现。
这里,T中“ ab”的出现如下:
1)“ ab”完全存在于字符串S出现的部分中,因此我们可以简单地找到Si中出现“ ab”的情况。将其设为C。因此,T中出现“ ab”的总次数为C * K。

2)否则,“ a”严格位于某个字符串Si中,而“ b”位于其他某个字符串Sj中(i ( KC 2 )中选择两次出现的字符串S,然后将其乘以no。 Si中出现“ a”的次数,而Sj中出现“ b”的次数。
As,Si = Sj = S.
时间复杂度: O(| S |),用于计数“ a”的数量和“ b”的数量。

C++
// CPP code to find number of subsequences of
// "ab" in the string S which is repeated K times.
#include 
using namespace std;
  
int countOccurrences(string s, int K)
{
    int n = s.length();
    int C, c1 = 0, c2 = 0;
    for (int i = 0; i < n; i++) {
        if (s[i] == 'a')
            c1++; // Count of 'a's
        if (s[i] == 'b') {
            c2++; // Count of 'b's
            C += c1; // occurrence of "ab"s in string S
        }
    }
  
    // Add following two :
    // 1) K * (Occurrences of "ab" in single string)
    // 2) a is from one string and b is from other.
    return C * K + (K * (K - 1) / 2) * c1 * c2;
}
  
// Driver code
int main()
{
    string S = "abcb";
    int k = 2;
    cout << countOccurrences(S, k) << endl;
    return 0;
}


Java
// Java code to find number of subsequences of
// "ab" in the string S which is repeated K times.
  
import java.io.*;
  
class GFG {
  
    static int countOccurrences(String s, int K)
    {
        int n = s.length();
        int C = 0, c1 = 0, c2 = 0;
        for (int i = 0; i < n; i++) {
            if (s.charAt(i) == 'a')
                c1++; // Count of 'a's
            if (s.charAt(i) == 'b') {
                c2++; // Count of 'b's
  
                // occurrence of "ab"s
                // in string S
                C += c1;
            }
        }
  
        // Add following two :
        // 1) K * (Occurrences of "ab" in single string)
        // 2) a is from one string and b is from other.
        return C * K + (K * (K - 1) / 2) * c1 * c2;
    }
  
    // Driver code
    public static void main(String[] args)
    {
        String S = "abcb";
        int k = 2;
  
        System.out.println(countOccurrences(S, k));
    }
}
  
// This code is contributed by vt_m.


Python3
# Python3 code to find number of 
# subsequences of "ab" in the 
# string S which is repeated K times.
  
def countOccurrences (s, K):
    n = len(s)
    c1 = 0
    c2 = 0
    C = 0
    for i in range(n):
        if s[i] == 'a':
            c1+= 1 # Count of 'a's
        if s[i] == 'b':
            c2+= 1 # Count of 'b's
              
            # occurrence of "ab"s in string S
            C += c1 
              
    # Add following two :
    # 1) K * (Occurrences of "ab" in single string)
    # 2) a is from one string and b is from other.
    return C * K + (K * (K - 1) / 2) * c1 * c2
      
      
# Driver code
S = "abcb"
k = 2
print (countOccurrences(S, k))
  
# This code is contributed by "Abhishek Sharma 44"


C#
// C# code to find number of subsequences
// of "ab" in the string S which is
// repeated K times.
using System;
  
class GFG {
  
    static int countOccurrences(string s, int K)
    {
  
        int n = s.Length;
        int C = 0, c1 = 0, c2 = 0;
  
        for (int i = 0; i < n; i++) {
  
            if (s[i] == 'a')
  
                // Count of 'a's
                c1++;
            if (s[i] == 'b') {
  
                // Count of 'b's
                c2++;
  
                // occurrence of "ab"s
                // in string S
                C += c1;
            }
        }
  
        // Add following two :
        // 1) K * (Occurrences of "ab" in
        // single string)
        // 2) a is from one string and b
        // is from other.
        return C * K + (K * (K - 1) / 2) * c1 * c2;
    }
  
    // Driver code
    public static void Main()
    {
  
        string S = "abcb";
        int k = 2;
  
        Console.WriteLine(
            countOccurrences(S, k));
    }
}
  
// This code is contributed by vt_m.


PHP


输出 :

6 

时间复杂度: O(| S |)。