📌  相关文章
📜  通过将两个连续的相同字符替换为新字符,可以对字符串进行计数

📅  最后修改于: 2021-04-29 05:43:32             🧑  作者: Mango

给定一个字符串str 。任务是计算所有可能不同的字符串的数量,如果该字符串的两个连续的相同的字符可以通过一个不同的字符来代替。

例子

方法:
观察到以下属性可一次替换两个等号字符:

  • 如果对于长度为3的字符串= “ aaa ”,我们用一个字符“ K”替换两个“ aa” ,则包括原始字符串在内的不同可能字符串的总数为: -Ka,aK,aaa 。因此不同字符串的数目如下Fibonacci数的字符串中的连续字符的长度的财产。
  • 如果对于字符串=“ aaadefyyyy ”,则通过一次替换两个连续的字符,可能的不同字符串的总数等于字符串“ aaa”“ yyyy”的组合的乘积。

因此,根据以上两个观察,具有第N个连续字符的不同可能字符串的计数由第N个斐波纳契数给出。因此,对于给定字符串str不同的可能的字符串的总数等于不同的可能字符串的计数与所有相同的字符每个子产品。

步骤如下:

  1. 计算(说cnt )在给定字符串str中相同的连续字符数。
  2. 要计算为计数CNT的不同可能的字符串,找到斐波那契序列的CNT值。
  3. 对给定字符串str中的所有连续字符重复上述步骤。
  4. 不同可能字符串的总计数等于每个连续字符计数所得到的斐波那契数列的所有值的乘积。

下面是上述方法的实现:

C++
// C++ program to count the
// different possible string
// form by replacing two same
// characters with one
#include 
using namespace std;
  
// Array to find the fibonacci
// sequence
int fib[100005];
  
// Function to find the
// fibonacci sequence
void computeFibonacci()
{
    fib[0] = 1;
    fib[1] = 1;
    for (int i = 2; i < 100005; i++) {
        fib[i] = fib[i - 1] + fib[i - 2];
    }
}
  
// Function to count all
// possible strings
int countString(string str)
{
  
    // Initialize ans = 1
    int ans = 1;
    int cnt = 1;
  
    for (int i = 1; str[i]; i++) {
  
        // If two consecutive
        // char are same
        // increase cnt
        if (str[i] == str[i - 1]) {
            cnt++;
        }
  
        // Else multiply the
        // fib[cnt] to ans
        // and initialize ans
        // to 1
        else {
            ans = ans * fib[cnt];
            cnt = 1;
        }
    }
    //
    // If str = abcdeeee, then
    // for last "eeee" the
    // count munst be updated
    ans = ans * fib[cnt];
  
    // Return the total count
    return ans;
}
  
// Driver's Code
int main()
{
    string str = "abdllldefkkkk";
  
    // Function to precompute
    // all the fibonacci number
    computeFibonacci();
  
    // Function call to find
    // the count
    cout << countString(str);
    return 0;
}


Java
// Java program to count the 
// different possible string 
// form by replacing two same 
// characters with one 
class GFG {
      
    // Array to find the fibonacci 
    // sequence 
    static int fib[] = new int[100005]; 
      
    // Function to find the 
    // fibonacci sequence 
    static void computeFibonacci() 
    { 
        fib[0] = 1; 
        fib[1] = 1; 
        for (int i = 2; i < 100005; i++) { 
            fib[i] = fib[i - 1] + fib[i - 2]; 
        } 
    } 
      
    // Function to count all 
    // possible strings 
    static int countString(String str) 
    { 
      
        // Initialize ans = 1 
        int ans = 1; 
        int cnt = 1; 
      
        for (int i = 1; i


Python3
# Python3 program to count the 
# different possible string 
# form by replacing two same 
# characters with one 
  
# Array to find the fibonacci 
# sequence 
fib = [0]*100005; 
  
# Function to find the 
# fibonacci sequence 
def computeFibonacci() :
  
    fib[0] = 1; 
    fib[1] = 1; 
    for i in range(2, 100005) :
        fib[i] = fib[i - 1] + fib[i - 2]; 
  
# Function to count all 
# possible strings 
def countString(string) :
  
    # Initialize ans = 1 
    ans = 1; 
    cnt = 1; 
  
    for i in range(1, len(string)) :
  
        # If two consecutive 
        # char are same 
        # increase cnt 
        if (string[i] == string[i - 1]) : 
            cnt += 1; 
  
        # Else multiply the 
        # fib[cnt] to ans 
        # and initialize ans 
        # to 1 
        else :
            ans = ans * fib[cnt]; 
            cnt = 1; 
              
    # If str = abcdeeee, then 
    # for last "eeee" the 
    # count munst be updated 
    ans = ans * fib[cnt]; 
  
    # Return the total count 
    return ans; 
  
# Driver's Code 
if __name__ == "__main__" : 
  
    string = "abdllldefkkkk"; 
  
    # Function to precompute 
    # all the fibonacci number 
    computeFibonacci(); 
  
    # Function call to find 
    # the count 
    print(countString(string)); 
      
# This code is contributed by Yash_R


C#
// C# program to count the 
// different possible string 
// form by replacing two same 
// characters with one 
using System;
  
class GFG {
      
    // Array to find the fibonacci 
    // sequence 
    static int []fib = new int[100005]; 
      
    // Function to find the 
    // fibonacci sequence 
    static void computeFibonacci() 
    { 
        fib[0] = 1; 
        fib[1] = 1; 
        for (int i = 2; i < 100005; i++) { 
            fib[i] = fib[i - 1] + fib[i - 2]; 
        } 
    } 
      
    // Function to count all 
    // possible strings 
    static int countString(string str) 
    { 
      
        // Initialize ans = 1 
        int ans = 1; 
        int cnt = 1; 
      
        for (int i = 1; i < str.Length; i++) { 
      
            // If two consecutive 
            // char are same 
            // increase cnt 
            if (str[i] == str[i - 1]) { 
                cnt++; 
            } 
      
            // Else multiply the 
            // fib[cnt] to ans 
            // and initialize ans 
            // to 1 
            else { 
                ans = ans * fib[cnt]; 
                cnt = 1; 
            } 
        } 
           
        // If str = abcdeeee, then 
        // for last "eeee" the 
        // count munst be updated 
        ans = ans * fib[cnt]; 
      
        // Return the total count 
        return ans; 
    } 
      
    // Driver's Code 
    public static void Main (string[] args) 
    { 
        string str = "abdllldefkkkk"; 
      
        // Function to precompute 
        // all the fibonacci number 
        computeFibonacci(); 
      
        // Function call to find 
        // the count 
        Console.WriteLine(countString(str)); 
    } 
}
  
// This code is contributed by AnkitRai01


输出:
15

时间复杂度: O(N),其中N是给定字符串的长度。