Given a string S and an array of equal length words (strings) arr[]. The task is to find the minimum start index of the substring that contains all the given words in a contiguous manner. If no such index is found, return -1.
Note: The words can be in any sequence.
Examples:
Input: arr[ ] = {“bat”, “bal”, “cat”}, S = “hellocatbyebalbatcatyo”
Output: 11
Explanation: Substring starting at index 11 contains all the 3 words in contiguous manner.
Input: arr[ ] = {“hat”, “mat”}, S = “aslkfndsuvbsdlvnsk”
Output: -1
Explanation: There is no substring that contains both the words in a contiguous manner.
Approach: The task can be solved by finding any of the words at the minimum index, and then simulating the process from the end index of the first word to check whether all other words are present at contiguous positions or not.
Follow the steps to solve the problem:
- Store all the words in an unordered set say st, to look-up the words in constant time.
- Traverse the string and check substring starting at each index of length M (length of each word), once a valid substring is found, that is present in S, start simulating the process after the end index of previous substring and check whether all other words are present in contiguous manner or not.
- If all words are found in contiguous manner, return the minimum index where it is found, else return -1.
Below is the implementation of the above approach:
C++14
#include <bits/stdc++.h>
using namespace std;
int minIndex(string arr[], string S, int N, int M)
{
unordered_set<string> st;
for ( int i = 0; i < N; i++)
st.insert(arr[i]);
for ( int i = 0; i < S.size() - M; i++) {
string x = S.substr(i, M);
int p = 0, k = -1, d = N;
if (st.find(x) != st.end()) {
k = i;
d--;
for ( int j = i + M; j < S.size() - M; j += M) {
string y = S.substr(j, M);
if (d == 0)
break ;
if (st.find(y) == st.end()) {
p = 1;
i = j - 1;
break ;
}
d--;
}
}
if (p == 0 and k != -1)
return k;
}
return -1;
}
int main()
{
string arr[] = { "bat" , "bal" , "cat" };
string S = "hellocatbyebalbatcatyo" ;
int N = sizeof (arr) / sizeof (arr[0]);
int M = arr[0].size();
cout << minIndex(arr, S, N, M);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int minIndex(String arr[], String S, int N, int M)
{
HashSet<String> st = new HashSet<String>();
for ( int i = 0 ; i < N; i++)
st.add(arr[i]);
for ( int i = 0 ; i < S.length() - M; i++)
{
String x = S.substring(i, i+M);
int p = 0 , k = - 1 , d = N;
if (st.contains(x)) {
k = i;
d--;
for ( int j = i + M; j < S.length() - M; j += M) {
String y = S.substring(j, j+M);
if (d == 0 )
break ;
if (!st.contains(y)) {
p = 1 ;
i = j - 1 ;
break ;
}
d--;
}
}
if (p == 0 && k != - 1 )
return k;
}
return - 1 ;
}
public static void main(String[] args)
{
String arr[] = { "bat" , "bal" , "cat" };
String S = "hellocatbyebalbatcatyo" ;
int N = arr.length;
int M = arr[ 0 ].length();
System.out.print(minIndex(arr, S, N, M));
}
}
|
Python3
def minIndex(arr, S, N, M):
st = set (arr)
for i in range ( len (S) - M):
x = S[i: i + M]
p = 0
k = - 1
d = N
if x in st:
k = i
d - = 1
for j in range (i + M, len (S) - M, M):
y = S[j: j + M]
if d = = 0 :
break
if y not in st:
p = 1
i = j - 1
break
d - = 1
if p = = 0 and k ! = - 1 :
return k
return - 1
arr = [ "bat" , "bal" , "cat" ]
S = "hellocatbyebalbatcatyo"
N = len (arr)
M = len (arr[ 0 ])
print (minIndex(arr, S, N, M))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int minIndex( string []arr, string S, int N, int M)
{
HashSet< string > st = new HashSet< string >();
for ( int i = 0; i < N; i++)
st.Add(arr[i]);
for ( int i = 0; i < S.Length - M; i++) {
string x = S.Substring(i, M);
int p = 0, k = -1, d = N;
if (st.Contains(x)) {
k = i;
d--;
for ( int j = i + M; j < S.Length - M; j += M) {
string y = S.Substring(j, M);
if (d == 0)
break ;
if (st.Contains(y)== false ) {
p = 1;
i = j - 1;
break ;
}
d--;
}
}
if (p == 0 && k != -1)
return k;
}
return -1;
}
public static void Main()
{
string []arr = { "bat" , "bal" , "cat" };
string S = "hellocatbyebalbatcatyo" ;
int N = arr.Length;
int M = arr[0].Length;
Console.Write(minIndex(arr, S, N, M));
}
}
|
Javascript
<script>
function minIndex(arr, S, N, M) {
let st = new Set();
for (let i = 0; i < N; i++) st.add(arr[i]);
for (let i = 0; i < S.length - M; i++) {
let x = S.substr(i, M);
console.log(x);
let p = 0,
k = -1,
d = N;
if (st.has(x)) {
k = i;
d--;
for (let j = i + M; j < S.length - M; j += M) {
let y = S.substr(j, M);
if (d == 0) break ;
if (!st.has(y)) {
p = 1;
i = j - 1;
break ;
}
d--;
}
}
if (p == 0 && k != -1) return k;
}
return -1;
}
let arr = [ "bat" , "bal" , "cat" ];
let S = "hellocatbyebalbatcatyo" ;
let N = arr.length;
let M = arr[0].length;
document.write(minIndex(arr, S, N, M));
</script>
|
Time Complexity: O(S*M), S is length of string and M is length of each word
Auxiliary Space: O(N), N is number of words
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
20 Sep, 2021
Like Article
Save Article