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 <bits/stdc++.h>
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] <<endl;
flag = 1;
}
}
// If no substring exist
if (flag == 0)
cout<<"-1"<<endl;
}
// Driver Code
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;
}
// This code is contributed by sanjoy_62.
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<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.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<String> 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
<script>
// JavaScript program of the above approach
// Function to print all the strings
// in the given array that occur as
// the substring in the given string
const isSubstr = (Str, arr) => {
// Stores if no string is a
// substring of str
let flag = 0;
// Iterate over the array of strings
for (i in arr)
{
// if the current string occur
// as a substring in Str
if (Str.indexOf(arr[i]) != -1)
{
// Print string i
document.write(`${arr[i]}<br/>`);
flag = 1;
}
}
// If no substring exist
if (flag == 0)
document.write(-1);
}
// Driver Code
let arr = ["forg", "geek", "ek", "dog", "sfo"];
let Str = "geeksforgeeks";
isSubstr(Str, arr)
// This code is contributed by rakeshsahni
</script>
Time Complexity: O(N2)
Auxiliary Space: O(1)
Similar Reads
Reverse the substrings of the given String according to the given Array of indices Given a string S and an array of indices A[], the task is to reverse the substrings of the given string according to the given Array of indices.Note: A[i] ? length(S), for all i.Examples: Input: S = "abcdef", A[] = {2, 5} Output: baedcf Explanation: Input: S = "abcdefghij", A[] = {2, 5} Output: baed
10 min read
Different substrings in a string that start and end with given strings Given a string s and two other strings begin and end, find the number of different substrings in the string which begin and end with the given begin and end strings. Examples: Input : s = "geeksforgeeks" begin = "geeks" end = "for" Output : 1 Input : s = "vishakha" begin = "h" end = "a" Output : 2 T
9 min read
Find all substrings that are anagrams of another substring of the string S Given a string S, the task is to find all the substrings in the string S which is an anagram of another different substring in the string S. The different substrings mean the substring starts at a different index. Examples: Input: S = "aba"Output: a a ab baExplanation:Following substrings are anagra
6 min read
Count the strings that are subsequence of the given string Given a string S and an array arr[] of words, the task is to return the number of words from the array which is a subsequence of S. Examples: Input: S = âprogrammingâ, arr[] = {"prom", "amin", "proj"}Output: 2Explanation: "prom" and "amin" are subsequence of S while "proj" is not) Input: S = âgeeksf
11 min read
Print all Substrings of length n possible from the given String Given a string str and an integer N, the task is to print all possible sub-strings of length N. Examples: Input: str = âgeeksforgeeksâ, N = 3Output: gee eek eks ksf sfo for org rge gee eek eksExplanations: All possible sub-strings of length 3 are âgeeâ, âeekâ, âeksâ, âksfâ, âsfoâ, âforâ, âorgâ, ârge
8 min read
Given a string and an integer k, find the kth sub-string when all the sub-strings are sorted according to the given condition Given a string str, its sub-strings are formed in such a way that all the sub-strings starting with the first character of the string will occur first in the sorted order of their lengths followed by all the sub-strings starting with the second character of the string in the sorted order of their le
9 min read