📜  计算给定字符串中长度为 3 的子序列

📅  最后修改于: 2022-05-13 01:57:08.578000             🧑  作者: Mango

计算给定字符串中长度为 3 的子序列

给定一个长度为 n 的字符串和一个长度为 3 的子序列。求子序列在该字符串中出现的总次数。

例子 :

Input : string = "GFGFGYSYIOIWIN", 
        subsequence = "GFG"
Output : 4
Explanation : There are 4 such subsequences as shown:
              GFGFGYSYIOIWIN
              GFGFGYSYIOIWIN
              GFGFGYSYIOIWIN
              GFGFGYSYIOIWIN

Input : string = "GFGGGZZYNOIWIN", 
        subsequence = "GFG"
Output : 3

蛮力方法:解决此问题的最简单方法是对给定子序列的树字符运行三个循环。即开始循环以查找字符串中子序列的第一个字符,一旦找到该字符,则在该索引之后开始循环以查找第二个字符,一旦找到该字符,则在该索引之后开始循环以查找第三个字符。维护一个变量来存储第三个字符的出现次数,即子序列的出现次数。

以下是上述方法的实现:

C++
// C++ program to find number of occurrences of
// a subsequence of length 3
#include
using namespace std;
 
// Function to find number of occurrences of
// a subsequence of length three in a string
int findOccurrences(string str, string substr)
{   
    // variable to store no of occurrences
    int counter = 0;
 
    // loop to find first character
    for (int i=0;i


Java
// Java program to find number of occurrences of
// a subsequence of length 3
import java.util.*;
import java.lang.*;
 
public class GfG{
     
    // Function to find number of occurrences of
    // a subsequence of length three in a string
    public static int findOccurrences(String str1, String substr1)
    {
        // variable to store no of occurrences
        int counter = 0;
         
        char[] str = str1.toCharArray();
        char[] substr = substr1.toCharArray();
         
        // loop to find first character
        for (int i=0; i < str1.length(); i++)
        {
            if (str[i] == substr[0])
            {
                // loop to find 2nd character
                for (int j = i+1; j < str1.length(); j++)
                {
                    if (str[j] == substr[1])
                    {
                        // loop to find 3rd character
                        for (int k = j+1; k < str1.length(); k++)
                        {
                            // increment count if subsequence
                            // is found
                            if (str[k] == substr[2])
                                counter++;
                        }
                    }
                }
            }
        }
 
        return counter;
    }
     
    // Driver function
    public static void main(String argc[]){
        String str = "GFGFGYSYIOIWIN";
        String substr = "GFG";
 
        System.out.println(findOccurrences(str, substr));
    }
     
}
/* This code is contributed by Sagar Shukla */


Python3
# Python program to find
# number of occurrences
# of a subsequence of
# length 3
 
# Function to find number
# of occurrences of a
# subsequence of length
# three in a string
def findOccurrences(str, substr) :
 
    # variable to store
    # no of occurrences
    counter = 0
 
    # loop to find
    # first character
    for i in range(0, len(str)) :
     
        if (str[i] == substr[0]) :
         
            # loop to find
            # 2nd character
            for j in range(i + 1,
                           len(str)) :
             
                if (str[j] == substr[1]) :
                 
                    # loop to find
                    # 3rd character
                    for k in range(j + 1,
                                   len(str)) :
                     
                        # increment count if
                        # subsequence is found
                        if (str[k] == substr[2]) :
                            counter = counter + 1
 
    return counter
 
# Driver Code
str = "GFGFGYSYIOIWIN"
substr = "GFG"
print (findOccurrences(str, substr))
 
# This code is contributed by
# Manish Shaw(manishshaw1)


C#
// C# program to find number of occurrences of
// a subsequence of length 3
using System;
 
public class GfG {
     
    // Function to find number of occurrences of
    // a subsequence of length three in a string
    public static int findOccurrences(string str1,
                                   string substr1)
    {
         
        // variable to store no of occurrences
        int counter = 0;
         
        //char[] str = str1.toCharArray();
        //char[] substr = substr1.toCharArray();
         
        // loop to find first character
        for (int i=0; i < str1.Length; i++)
        {
            if (str1[i] == substr1[0])
            {
                 
                // loop to find 2nd character
                for (int j = i+1; j < str1.Length; j++)
                {
                    if (str1[j] == substr1[1])
                    {
                         
                        // loop to find 3rd character
                        for (int k = j+1; k < str1.Length; k++)
                        {
                             
                            // increment count if subsequence
                            // is found
                            if (str1[k] == substr1[2])
                                counter++;
                        }
                    }
                }
            }
        }
 
        return counter;
    }
     
    // Driver function
    public static void Main()
    {
        string str1 = "GFGFGYSYIOIWIN";
        string substr1 = "GFG";
 
        Console.WriteLine(findOccurrences(str1, substr1));
    }
}
 
/* This code is contributed by vt_m */


PHP


Javascript


C++
// C++ program to find number of occurrences of
// a subsequence of length 3
#include
using namespace std;
 
// Function to find number of occurrences of
// a subsequence of length three in a string
int findOccurrences(string str, string substr)
{   
    // calculate length of string
    int n = str.length();
 
    // auxiliary array to store occurrences of
    // first character
    int preLeft[n] = {0};
 
    // auxiliary array to store occurrences of
    // third character
    int preRight[n] = {0};
 
    if (str[0]==substr[0])
        preLeft[0]++;
 
    // calculate occurrences of first character
    // upto ith index from left
    for (int i=1;i=0;i--)
    {
        if (str[i]==substr[2])       
            preRight[i] = preRight[i+1] + 1;       
        else       
            preRight[i] = preRight[i+1];       
    }
 
    // variable to store total number of occurrences
    int counter = 0;
 
    // loop to find the occurrences of middle element
    for (int i=1; i


Java
// Java program to find number of occurrences of
// a subsequence of length 3
import java.util.*;
import java.lang.*;
 
public class GfG{
     
    // Function to find number of occurrences of
    // a subsequence of length three in a string
    public static int findOccurrences(String str1, String substr1)
    {
        // calculate length of string
        int n = str1.length();
         
        char[] str = str1.toCharArray();
        char[] substr = substr1.toCharArray();
         
        // auxiliary array to store occurrences of
        // first character
        int[] preLeft = new int[n];
 
        // auxiliary array to store occurrences of
        // third character
        int[] preRight = new int[n];
 
        if (str[0] == substr[0])
            preLeft[0]++;
 
        // calculate occurrences of first character
        // upto ith index from left
        for (int i = 1; i < n; i++)
        {
            if (str[i] == substr[0])    
                preLeft[i] = preLeft[i-1] + 1;    
            else
                preLeft[i] = preLeft[i-1];    
        }
 
        if (str[n-1] == substr[2])
            preRight[n-1]++;
 
        // calculate occurrences of third character
        // upto ith index from right
        for(int i = n-2; i >= 0; i--)
        {
            if (str[i] == substr[2])    
                preRight[i] = preRight[i+1] + 1;    
            else   
                preRight[i] = preRight[i+1];    
        }
 
        // variable to store total number of occurrences
        int counter = 0;
 
        // loop to find the occurrences of middle element
        for (int i = 1; i < n-1; i++)
        {
            // if middle character of subsequence is found
            // in the string
            if (str[i] == str[1])
            {
                // multiply the total occurrences of first
                // character before middle character with
                // the total occurrences of third character
                // after middle character
                int total = preLeft[i-1] * preRight[i+1];
                counter += total;
            }
        }
 
        return counter;
    }
     
    // Driver function
    public static void main(String argc[]){
        String str = "GFGFGYSYIOIWIN";
        String substr = "GFG";
         
        System.out.println(findOccurrences(str, substr));
    }
     
    /* This code is contributed by Sagar Shukla */
}


Python3
# Python 3 program to find number of
# occurrences of a subsequence of length 3
 
# Function to find number of occurrences of
# a subsequence of length three in a string
def findOccurrences(str1, substr):
     
    # calculate length of string
    n = len(str1)
 
    # auxiliary array to store occurrences of
    # first character
    preLeft = [0 for i in range(n)]
 
    # auxiliary array to store occurrences of
    # third character
    preRight = [0 for i in range(n)]
 
    if (str1[0] == substr[0]):
        preLeft[0] += 1
 
    # calculate occurrences of first character
    # upto ith index from left
    for i in range(1, n):
        if (str1[i] == substr[0]):
            preLeft[i] = preLeft[i - 1] + 1   
        else:
            preLeft[i] = preLeft[i - 1]    
 
    if (str1[n - 1] == substr[2]):
        preRight[n - 1] += 1
 
    # calculate occurrences of third character
    # upto ith index from right
    i = n - 2
    while(i >= 0):
        if (str1[i] == substr[2]):
            preRight[i] = preRight[i + 1] + 1   
        else:    
            preRight[i] = preRight[i + 1]
 
        i -= 1
 
    # variable to store
    # total number of occurrences
    counter = 0
 
    # loop to find the occurrences
    # of middle element
    for i in range(1, n - 1):
         
        # if middle character of subsequence
        # is found in the string
        if (str1[i] == str1[1]):
             
            # multiply the total occurrences of first
            # character before middle character with
            # the total occurrences of third character
            # after middle character
            total = preLeft[i - 1] * preRight[i + 1]
            counter += total
 
    return counter
 
# Driver code
if __name__ == '__main__':
    str1 = "GFGFGYSYIOIWIN"
    substr = "GFG"
    print(findOccurrences(str1, substr))
     
# This code is contributed by
# Surendra_Gangwar


C#
// C# program to find number of occurrences of
// a subsequence of length 3
using System;
 
public class GfG {
     
    // Function to find number of occurrences of
    // a subsequence of length three in a string
    public static int findOccurrences(string str1,
                                   string substr1)
    {
         
        // calculate length of string
        int n = str1.Length;
         
        // char[] str = str1.toCharArray();
        // char[] substr = substr1.toCharArray();
         
        // auxiliary array to store occurrences of
        // first character
        int[] preLeft = new int[n];
 
        // auxiliary array to store occurrences of
        // third character
        int[] preRight = new int[n];
 
        if (str1[0] == substr1[0])
            preLeft[0]++;
 
        // calculate occurrences of first character
        // upto ith index from left
        for (int i = 1; i < n; i++)
        {
            if (str1[i] == substr1[0])
                preLeft[i] = preLeft[i-1] + 1;
            else
                preLeft[i] = preLeft[i-1];
        }
 
        if (str1[n-1] == substr1[2])
            preRight[n-1]++;
 
        // calculate occurrences of third character
        // upto ith index from right
        for(int i = n-2; i >= 0; i--)
        {
            if (str1[i] == substr1[2])
                preRight[i] = preRight[i+1] + 1;    
            else
                preRight[i] = preRight[i+1];    
        }
 
        // variable to store total number of occurrences
        int counter = 0;
 
        // loop to find the occurrences of middle element
        for (int i = 1; i < n-1; i++)
        {
             
            // if middle character of subsequence is found
            // in the string
            if (str1[i] == str1[1])
            {
                 
                // multiply the total occurrences of first
                // character before middle character with
                // the total occurrences of third character
                // after middle character
                int total = preLeft[i-1] * preRight[i+1];
                counter += total;
            }
        }
 
        return counter;
    }
     
    // Driver function
    public static void Main()
    {
        string str1 = "GFGFGYSYIOIWIN";
        string substr1 = "GFG";
         
        Console.WriteLine(findOccurrences(str1, substr1));
    }
}
 
/* This code is contributed by Vt_m */


PHP
= 0; $i--)
    {
        if ($str[$i] == $substr[2])
            $preRight[$i] = ($preRight[$i + 1] + 1);
        else
            $preRight[$i] = $preRight[$i + 1];    
    }
 
    // variable to store total
    // number of occurrences
    $counter = 0;
 
    // loop to find the occurrences
    // of middle element
    for ($i = 1; $i < $n - 1; $i++)
    {
        // if middle character of
        // subsequence is found
        // in the string
        if ($str[$i] == $str[1])
        {
            // multiply the total occurrences
            // of first character before
            // middle character with the
            // total occurrences of third
            // character after middle character
            $total = $preLeft[$i - 1] *
                     $preRight[$i + 1];
            $counter += $total;
        }
    }
 
    return $counter;
}
 
// Driver Code
$str = "GFGFGYSYIOIWIN";
$substr = "GFG";
echo findOccurrences($str, $substr);
 
// This code is contributed by aj_36
?>


Javascript


输出 :

4

时间复杂度:O(n^3)
辅助空间:O(1)

有效方法:解决这个问题的有效方法是使用预计算的概念。这个想法是对于字符串中子序列的中间字符的每次出现,预先计算两个值。也就是说,它之前的第一个字符的出现次数和它之后的第三个字符的出现次数,并将这两个值相乘以生成每个中间字符出现的总出现次数。

下面是这个想法的实现:

C++

// C++ program to find number of occurrences of
// a subsequence of length 3
#include
using namespace std;
 
// Function to find number of occurrences of
// a subsequence of length three in a string
int findOccurrences(string str, string substr)
{   
    // calculate length of string
    int n = str.length();
 
    // auxiliary array to store occurrences of
    // first character
    int preLeft[n] = {0};
 
    // auxiliary array to store occurrences of
    // third character
    int preRight[n] = {0};
 
    if (str[0]==substr[0])
        preLeft[0]++;
 
    // calculate occurrences of first character
    // upto ith index from left
    for (int i=1;i=0;i--)
    {
        if (str[i]==substr[2])       
            preRight[i] = preRight[i+1] + 1;       
        else       
            preRight[i] = preRight[i+1];       
    }
 
    // variable to store total number of occurrences
    int counter = 0;
 
    // loop to find the occurrences of middle element
    for (int i=1; i

Java

// Java program to find number of occurrences of
// a subsequence of length 3
import java.util.*;
import java.lang.*;
 
public class GfG{
     
    // Function to find number of occurrences of
    // a subsequence of length three in a string
    public static int findOccurrences(String str1, String substr1)
    {
        // calculate length of string
        int n = str1.length();
         
        char[] str = str1.toCharArray();
        char[] substr = substr1.toCharArray();
         
        // auxiliary array to store occurrences of
        // first character
        int[] preLeft = new int[n];
 
        // auxiliary array to store occurrences of
        // third character
        int[] preRight = new int[n];
 
        if (str[0] == substr[0])
            preLeft[0]++;
 
        // calculate occurrences of first character
        // upto ith index from left
        for (int i = 1; i < n; i++)
        {
            if (str[i] == substr[0])    
                preLeft[i] = preLeft[i-1] + 1;    
            else
                preLeft[i] = preLeft[i-1];    
        }
 
        if (str[n-1] == substr[2])
            preRight[n-1]++;
 
        // calculate occurrences of third character
        // upto ith index from right
        for(int i = n-2; i >= 0; i--)
        {
            if (str[i] == substr[2])    
                preRight[i] = preRight[i+1] + 1;    
            else   
                preRight[i] = preRight[i+1];    
        }
 
        // variable to store total number of occurrences
        int counter = 0;
 
        // loop to find the occurrences of middle element
        for (int i = 1; i < n-1; i++)
        {
            // if middle character of subsequence is found
            // in the string
            if (str[i] == str[1])
            {
                // multiply the total occurrences of first
                // character before middle character with
                // the total occurrences of third character
                // after middle character
                int total = preLeft[i-1] * preRight[i+1];
                counter += total;
            }
        }
 
        return counter;
    }
     
    // Driver function
    public static void main(String argc[]){
        String str = "GFGFGYSYIOIWIN";
        String substr = "GFG";
         
        System.out.println(findOccurrences(str, substr));
    }
     
    /* This code is contributed by Sagar Shukla */
}

Python3

# Python 3 program to find number of
# occurrences of a subsequence of length 3
 
# Function to find number of occurrences of
# a subsequence of length three in a string
def findOccurrences(str1, substr):
     
    # calculate length of string
    n = len(str1)
 
    # auxiliary array to store occurrences of
    # first character
    preLeft = [0 for i in range(n)]
 
    # auxiliary array to store occurrences of
    # third character
    preRight = [0 for i in range(n)]
 
    if (str1[0] == substr[0]):
        preLeft[0] += 1
 
    # calculate occurrences of first character
    # upto ith index from left
    for i in range(1, n):
        if (str1[i] == substr[0]):
            preLeft[i] = preLeft[i - 1] + 1   
        else:
            preLeft[i] = preLeft[i - 1]    
 
    if (str1[n - 1] == substr[2]):
        preRight[n - 1] += 1
 
    # calculate occurrences of third character
    # upto ith index from right
    i = n - 2
    while(i >= 0):
        if (str1[i] == substr[2]):
            preRight[i] = preRight[i + 1] + 1   
        else:    
            preRight[i] = preRight[i + 1]
 
        i -= 1
 
    # variable to store
    # total number of occurrences
    counter = 0
 
    # loop to find the occurrences
    # of middle element
    for i in range(1, n - 1):
         
        # if middle character of subsequence
        # is found in the string
        if (str1[i] == str1[1]):
             
            # multiply the total occurrences of first
            # character before middle character with
            # the total occurrences of third character
            # after middle character
            total = preLeft[i - 1] * preRight[i + 1]
            counter += total
 
    return counter
 
# Driver code
if __name__ == '__main__':
    str1 = "GFGFGYSYIOIWIN"
    substr = "GFG"
    print(findOccurrences(str1, substr))
     
# This code is contributed by
# Surendra_Gangwar

C#

// C# program to find number of occurrences of
// a subsequence of length 3
using System;
 
public class GfG {
     
    // Function to find number of occurrences of
    // a subsequence of length three in a string
    public static int findOccurrences(string str1,
                                   string substr1)
    {
         
        // calculate length of string
        int n = str1.Length;
         
        // char[] str = str1.toCharArray();
        // char[] substr = substr1.toCharArray();
         
        // auxiliary array to store occurrences of
        // first character
        int[] preLeft = new int[n];
 
        // auxiliary array to store occurrences of
        // third character
        int[] preRight = new int[n];
 
        if (str1[0] == substr1[0])
            preLeft[0]++;
 
        // calculate occurrences of first character
        // upto ith index from left
        for (int i = 1; i < n; i++)
        {
            if (str1[i] == substr1[0])
                preLeft[i] = preLeft[i-1] + 1;
            else
                preLeft[i] = preLeft[i-1];
        }
 
        if (str1[n-1] == substr1[2])
            preRight[n-1]++;
 
        // calculate occurrences of third character
        // upto ith index from right
        for(int i = n-2; i >= 0; i--)
        {
            if (str1[i] == substr1[2])
                preRight[i] = preRight[i+1] + 1;    
            else
                preRight[i] = preRight[i+1];    
        }
 
        // variable to store total number of occurrences
        int counter = 0;
 
        // loop to find the occurrences of middle element
        for (int i = 1; i < n-1; i++)
        {
             
            // if middle character of subsequence is found
            // in the string
            if (str1[i] == str1[1])
            {
                 
                // multiply the total occurrences of first
                // character before middle character with
                // the total occurrences of third character
                // after middle character
                int total = preLeft[i-1] * preRight[i+1];
                counter += total;
            }
        }
 
        return counter;
    }
     
    // Driver function
    public static void Main()
    {
        string str1 = "GFGFGYSYIOIWIN";
        string substr1 = "GFG";
         
        Console.WriteLine(findOccurrences(str1, substr1));
    }
}
 
/* This code is contributed by Vt_m */

PHP

= 0; $i--)
    {
        if ($str[$i] == $substr[2])
            $preRight[$i] = ($preRight[$i + 1] + 1);
        else
            $preRight[$i] = $preRight[$i + 1];    
    }
 
    // variable to store total
    // number of occurrences
    $counter = 0;
 
    // loop to find the occurrences
    // of middle element
    for ($i = 1; $i < $n - 1; $i++)
    {
        // if middle character of
        // subsequence is found
        // in the string
        if ($str[$i] == $str[1])
        {
            // multiply the total occurrences
            // of first character before
            // middle character with the
            // total occurrences of third
            // character after middle character
            $total = $preLeft[$i - 1] *
                     $preRight[$i + 1];
            $counter += $total;
        }
    }
 
    return $counter;
}
 
// Driver Code
$str = "GFGFGYSYIOIWIN";
$substr = "GFG";
echo findOccurrences($str, $substr);
 
// This code is contributed by aj_36
?>

Javascript


输出 :

4

时间复杂度:O(n)
辅助空间:O(n)