Open in App
Not now

# Print all strings in the given array that occur as the substring in the given string

• Last Updated : 29 Mar, 2022

Given an array of string arr[] and a string str, the task is to print all the strings in arr[] that occur as a substring in str.

Example:

Input: str =”geeksforgeeks”, arr[] ={ “forg”, “geek”, “ek”, “dog”, “sfor”}
Output:
forg
geek
ek
sfor
Explanation: The strings “forg”, “geek”, “ek” and “sfor” occur as a substring in str. Therefore, the required count is 4.

Input: str =”abcd”, arr[] ={ “aa”, “bb”, “cc”}
Output: -1

Approach: The given problem is an implementation base problem. It can be solved by iterating over the given array of strings and for each string in arr[], check whether it occurs as a substring of str or not using the algorithm discussed in this article. Maintain a variable that stores If no string exists as a substring. In that case, print -1.

Below is the implementation of the above approach:

## 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

 ``

Output

```forg
geek
ek
sfo```

Time Complexity: O(N2)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up
Related Articles