📌  相关文章
📜  包含另一个给定字符串作为子字符串的子字符串计数

📅  最后修改于: 2021-09-03 13:53:55             🧑  作者: Mango

给定两个字符串ST ,任务是计算S中包含字符串T作为子字符串的子字符串的数量。

例子:

方法:想法是生成S的所有子串并检查每个子串是否包含T。请按照以下步骤解决问题:

  • 初始化一个变量,比如count ,用0来存储所需子串的数量。
  • 查找给定字符串S 的所有子字符串 并将它们存储在辅助数组arr[] 中
  • 遍历给定的数组arr[]并针对其中的每个字符串,检查字符串T 是否存在于该字符串。如果T出现,则将计数增加1
  • 完成上述步骤后, count的值就是得到的count。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to store all substrings of S
vector subString(string s, int n)
{
    // Stores the substrings of S
    vector v;
 
    // Pick start point in outer loop
    // and lengths of different strings
    // for a given starting point
    for (int i = 0; i < n; i++) {
 
        for (int len = 1;
             len <= n - i; len++) {
 
            string find = s.substr(i, len);
            v.push_back(find);
        }
    }
 
    // Return the array containing
    // substrings of S
    return v;
}
 
// Function to check if a string is
// present in another string
int IsPresent(string& str, string& target)
{
    // Check if target is in the
    // string str or not
    if (str.find(target)
        != string::npos) {
        return 1;
    }
 
    return -1;
}
 
// Function to count the substring of S
// containing T in it as substring
void countSubstrings(string& S, string& T)
{
 
    // Store all substrings of S in
    // the array v[]
    vector v = subString(S, S.length());
 
    // Store required count of substrings
    int ans = 0;
 
    // Iterate through all the
    // substrings of S
    for (auto it : v) {
 
        // If string T is present in the
        // current substring, then
        // increment the ans
        if (IsPresent(it, T) != -1) {
            ans++;
        }
    }
 
    // Print the answer
    cout << ans;
}
 
// Driver code
int main()
{
    string S = "dabc";
    string T = "ab";
 
    // Function Call
    countSubstrings(S, T);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
class GFG
{
 
// Function to store all subStrings of S
static Vector subString(String s, int n)
{
   
    // Stores the subStrings of S
    Vector v = new Vector<>();
 
    // Pick start point in outer loop
    // and lengths of different Strings
    // for a given starting point
    for (int i = 0; i < n; i++)
    {
 
        for (int len = 1;
             len <= n - i; len++)
        {
 
            String find = s.substring(i, i + len);
            v.add(find);
        }
    }
 
    // Return the array containing
    // subStrings of S
    return v;
}
 
// Function to check if a String is
// present in another String
static int IsPresent(String str, String target)
{
   
    // Check if target is in the
    // String str or not
    if (str.contains(target))
    {
        return 1;
    }
    return -1;
}
 
// Function to count the subString of S
// containing T in it as subString
static void countSubStrings(String S, String T)
{
 
    // Store all subStrings of S in
    // the array v[]
    Vector v = subString(S, S.length());
 
    // Store required count of subStrings
    int ans = 0;
 
    // Iterate through all the
    // subStrings of S
    for (String it : v)
    {
 
        // If String T is present in the
        // current subString, then
        // increment the ans
        if (IsPresent(it, T) != -1)
        {
            ans++;
        }
    }
 
    // Print the answer
    System.out.print(ans);
}
 
// Driver code
public static void main(String[] args)
{
    String S = "dabc";
    String T = "ab";
 
    // Function Call
    countSubStrings(S, T);
 
}
}
 
// This code is contributed by Princi Singh


Python3
# Python3 program for the above approach
 
# Function to store all substrings of S
def subString(s, n):
     
    # Stores the substrings of S
    v = []
 
    # Pick start point in outer loop
    # and lengths of different strings
    # for a given starting point
    for i in range(n):
 
        for len in range(1, n - i + 1):
 
            find = s[i : i + len]
            v.append(find)
 
    # Return the array containing
    # substrings of S
    return v
 
# Function to check if a is
# present in another string
def IsPresent(str, target):
     
    # Check if target is in the
    # str or not
    if (target in str):
        return 1
 
    return -1
 
# Function to count the subof S
# containing T in it as substring
def countSubstrings(S, T):
 
    # Store all substrings of S in
    # the array v[]
    v = subString(S, len(S))
 
    # Store required count of substrings
    ans = 0
 
    # Iterate through all the
    # substrings of S
    for it in v:
 
        # If T is present in the
        # current substring, then
        # increment the ans
        if (IsPresent(it, T) != -1):
            ans += 1
 
    # Print the answer
    print(ans)
 
# Driver code
if __name__ == '__main__':
    S = "dabc"
    T = "ab"
 
    #Function Call
    countSubstrings(S, T)
 
# This code is contributed by mohit kumar 29


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG
{
 
    // Function to store all subStrings of S
    static List subString(string s, int n)
    {
 
        // Stores the subStrings of S
        List v = new List();
 
        // Pick start point in outer loop
        // and lengths of different Strings
        // for a given starting point
        for (int i = 0; i < n; i++)
        {
            for (int len = 1; len <= n - i; len++)
            {
                string find = s.Substring(i, len);
                v.Add(find);
            }
        }
 
        // Return the array containing
        // subStrings of S
        return v;
    }
 
    // Function to check if a String is
    // present in another String
    static int IsPresent(string str, string target)
    {
 
        // Check if target is in the
        // String str or not
        if (str.Contains(target))
        {
            return 1;
        }
        return -1;
    }
 
    // Function to count the subString of S
    // containing T in it as subString
    static void countSubStrings(string S, string T)
    {
 
        // Store all subStrings of S in
        // the array v[]
        List v = subString(S, S.Length);
 
        // Store required count of subStrings
        int ans = 0;
 
        // Iterate through all the
        // subStrings of S
        foreach(string it in v)
        {
 
            // If String T is present in the
            // current subString, then
            // increment the ans
            if (IsPresent(it, T) != -1)
            {
                ans++;
            }
        }
 
        // Print the answer
        Console.WriteLine(ans);
    }
 
    // Driver code
    public static void Main(string[] args)
    {
        string S = "dabc";
        string T = "ab";
 
        // Function Call
        countSubStrings(S, T);
    }
}
 
// This code is contributed by chitranayal


Javascript


输出:
4

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live