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++
#include <bits/stdc++.h>
using namespace std;
int isSubstring(string s1, string s2)
{
int M = s1.length();
int N = s2.length();
for ( int i = 0; i <= N - M; i++) {
int j;
for (j = 0; j < M; j++)
if (s2[i + j] != s1[j])
break ;
if (j == M)
return i;
}
return -1;
}
void isSubstr(string Str, string arr[], int len)
{
int flag = 0;
for ( int i = 0; i < len; i++)
{
int s = isSubstring(arr[i],Str);
if (s != -1) {
cout << arr[i] <<endl;
flag = 1;
}
}
if (flag == 0)
cout<< "-1" <<endl;
}
int main()
{
string arr[5]
= { "forg" , "geek" , "ek" , "dog" , "sfo" };
int len = sizeof (arr)/ sizeof (arr[0]);
string Str = "geeksforgeeks" ;
isSubstr(Str, arr, len);
return 0;
}
|
Java
import java.util.*;
class GFG
{
public static void isSubstr(String Str,
ArrayList<String> arr)
{
int flag = 0 ;
for ( int i = 0 ; i < arr.size(); i++)
{
if (Str.indexOf(arr.get(i)) != - 1 ) {
System.out.println(arr.get(i));
flag = 1 ;
}
}
if (flag == 0 )
System.out.print(- 1 );
}
public static void main(String[] args)
{
ArrayList<String> arr
= new ArrayList<>(Arrays.asList(
"forg" , "geek" , "ek" , "dog" , "sfo" ));
String Str = "geeksforgeeks" ;
isSubstr(Str, arr);
}
}
|
Python3
def isSubstr( Str , arr):
flag = 0
for i in arr:
if i in Str :
print (i)
flag = 1
if flag = = 0 :
print ( - 1 )
arr = [ "forg" , "geek" , "ek" , "dog" , "sfo" ]
Str = "geeksforgeeks"
isSubstr( Str , arr)
|
C#
using System;
public class GFG
{
public static void isSubstr(String Str,String[] arr)
{
int flag = 0;
for ( int i = 0; i < arr.Length; i++)
{
if (Str.IndexOf(arr[i]) != -1) {
Console.WriteLine(arr[i]);
flag = 1;
}
}
if (flag == 0)
Console.Write(-1);
}
public static void Main(String[] args)
{
String[] arr = { "forg" , "geek" , "ek" , "dog" , "sfo" };
String Str = "geeksforgeeks" ;
isSubstr(Str, arr);
}
}
|
Javascript
<script>
const isSubstr = (Str, arr) => {
let flag = 0;
for (i in arr)
{
if (Str.indexOf(arr[i]) != -1)
{
document.write(`${arr[i]}<br/>`);
flag = 1;
}
}
if (flag == 0)
document.write(-1);
}
let arr = [ "forg" , "geek" , "ek" , "dog" , "sfo" ];
let Str = "geeksforgeeks" ;
isSubstr(Str, arr)
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...