📌  相关文章
📜  打印给定数组中作为给定字符串中的子字符串出现的所有字符串

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

打印给定数组中作为给定字符串中的子字符串出现的所有字符串

给定字符串arr[]和字符串str的数组,任务是打印arr[]中作为str中的子字符串出现的所有字符串。

例子:

方法:给定的问题是一个实现基础问题。它可以通过迭代给定的字符串数组来解决,对于arr[]中的每个字符串,使用本文讨论的算法检查它是否作为 str 的子字符串出现。维护一个变量,存储如果没有字符串存在。在这种情况下,打印 -1。

下面是上述方法的实现:

C++
// C++ program of the above approach
#include 
using namespace std;
 
// Returns true if s1 is substring of s2
int isSubstring(string s1, string s2)
{
  int M = s1.length();
  int N = s2.length();
 
  /* A loop to slide pat[] one by one */
  for (int i = 0; i <= N - M; i++) {
    int j;
 
    /* For current index i, check for
        pattern match */
    for (j = 0; j < M; j++)
      if (s2[i + j] != s1[j])
        break;
 
    if (j == M)
      return i;
  }
 
  return -1;
}
 
// Function to print all the strings
// in the given array that occur as
// the substring in the given string
void isSubstr(string Str, string arr[], int len)
{
 
  // Stores if no string is a
  // substring of str
  int flag = 0;
 
  // Iterate over the array of strings
  for (int i = 0; i < len; i++)
  {
 
    // if the current string occur
    // as a substring in Str
    int s = isSubstring(arr[i],Str);
    if (s != -1) {
 
      // Print string i
      cout << arr[i] <


Java
// JAVA program of the above approach
import java.util.*;
class GFG
{
   
    // Function to print all the strings
    // in the given array that occur as
    // the substring in the given string
    public static void isSubstr(String Str,
                                ArrayList arr)
    {
       
        // Stores if no string is a
        // substring of str
        int flag = 0;
 
        // Iterate over the array of strings
        for (int i = 0; i < arr.size(); i++)
        {
           
            // if the current string occur
            // as a substring in Str
            if (Str.indexOf(arr.get(i)) != -1) {
 
                // Print string i
                System.out.println(arr.get(i));
                flag = 1;
            }
        }
       
        // If no substring exist
        if (flag == 0)
            System.out.print(-1);
    }
   
    // Driver Code
    public static void main(String[] args)
    {
        ArrayList arr
            = new ArrayList<>(Arrays.asList(
                "forg", "geek", "ek", "dog", "sfo"));
        String Str = "geeksforgeeks";
 
        isSubstr(Str, arr);
    }
}
 
// This code is contributed by Taranpreet


Python3
# Python program of the above approach
 
# Function to print all the strings
# in the given array that occur as
# the substring in the given string
 
 
def isSubstr(Str, arr):
 
    # Stores if no string is a
    # substring of str
    flag = 0
 
    # Iterate over the array of strings
    for i in arr:
 
        # if the current string occur
        # as a substring in Str
        if i in Str:
 
            # Print string i
            print(i)
            flag = 1
 
    # If no substring exist
    if flag == 0:
        print(-1)
 
 
# Driver Code
arr = ["forg", "geek", "ek", "dog", "sfo"]
Str = "geeksforgeeks"
 
isSubstr(Str, arr)


C#
// C# program of the above approach
using System;
public class GFG
{
 
  // Function to print all the strings
  // in the given array that occur as
  // the substring in the given string
  public static void isSubstr(String Str,String[] arr)
  {
 
    // Stores if no string is a
    // substring of str
    int flag = 0;
 
    // Iterate over the array of strings
    for (int i = 0; i < arr.Length; i++)
    {
 
      // if the current string occur
      // as a substring in Str
      if (Str.IndexOf(arr[i]) != -1) {
 
        // Print string i
        Console.WriteLine(arr[i]);
        flag = 1;
      }
    }
 
    // If no substring exist
    if (flag == 0)
      Console.Write(-1);
  }
 
  // Driver Code
  public static void Main(String[] args)
  {
    String[] arr = {"forg", "geek", "ek", "dog", "sfo"};
    String Str = "geeksforgeeks";
 
    isSubstr(Str, arr);
  }
}
 
// This code is contributed by 29AjayKumar


Javascript


输出
forg
geek
ek
sfo

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