Check if given String is prefix subarray of the given Array
Last Updated :
12 May, 2022
Given a string str and an array of words word[], the task is to find whether str is a prefix string of word[].
Examples:
Input: str = “indiaismycountry”,
word[] = {“india”, “is”, “my”, “country”, “and”, “i”, “love”, “india”}
Output: true
Explanation: String str can be made by concatenating “india”, “is”, “my” and “country” together.
Input: str = “indianism”,
word[] = {“india”, “is”, “my”, “country”, “and”, “i”, “love”, “india”}
Output: false
Explanation: It is impossible to make str using the prefixes of the word array.
Approach: This is a simple implementation related problem. Follow the steps mentioned below:
- Take an empty string named ans.
- Iterate over the word array and keep on adding each element of the word array to ans.
- After adding to ans comparing it with the s, if they both match simply return true else continue.
- If the iteration ends and ans doesn’t matches with the s then return false.
Below is the C++ program to implement the above approach-
C++
#include <bits/stdc++.h>
using namespace std;
bool isPrefixString(string s,
vector<string>& word)
{
string ans = "" ;
int N = word.size();
for ( int i = 0; i < N; i++) {
ans += word[i];
if (ans == s)
return true ;
}
return false ;
}
int main()
{
string str = "indiaismycountry" ;
vector<string> word
= { "india" , "is" , "my" ,
"country" , "and" , "i" ,
"love" , "india" };
bool ans = isPrefixString(str, word);
if (ans)
cout << "True" ;
else
cout << "False" ;
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
static Boolean isPrefixString(String s,
String word[])
{
String ans = "" ;
int N = word.length;
for ( int i = 0 ; i < N; i++) {
ans += word[i];
if (ans.equals(s))
return true ;
}
return false ;
}
public static void main (String[] args)
{
String str = "indiaismycountry" ;
String word[]
= { "india" , "is" , "my" ,
"country" , "and" , "i" ,
"love" , "india" };
Boolean ans = isPrefixString(str, word);
if (ans)
System.out.println( "True" );
else
System.out.println( "False" );
}
}
|
Python
def isPrefixString(s, word):
ans = ""
N = len (word)
for i in range ( 0 , N):
ans = ans + (word[i])
if (ans = = s):
return True
return False
str = "indiaismycountry"
word = [ "india" , "is" , "my" , "country" , "and" , "i" , "love" , "india" ]
ans = isPrefixString( str , word)
if (ans = = True ):
print ( "True" )
else :
print ( "False" )
|
C#
using System;
class GFG {
static bool isPrefixString( string s,
string []word)
{
string ans = "" ;
int N = word.Length;
for ( int i = 0; i < N; i++)
{
ans += word[i];
if (ans.Equals(s))
return true ;
}
return false ;
}
public static void Main ()
{
string str = "indiaismycountry" ;
string []word
= { "india" , "is" , "my" ,
"country" , "and" , "i" ,
"love" , "india" };
bool ans = isPrefixString(str, word);
if (ans)
Console.WriteLine( "True" );
else
Console.WriteLine( "False" );
}
}
|
Javascript
<script>
function isPrefixString(s, word)
{
let ans = "" ;
let N = word.length;
for (let i = 0; i < N; i++)
{
ans += word[i];
if (ans == s)
return true ;
}
return false ;
}
let str = "indiaismycountry" ;
let word
= [ "india" , "is" , "my" ,
"country" , "and" , "i" ,
"love" , "india" ];
let ans = isPrefixString(str, word);
if (ans)
document.write( "True" );
else
document.write( "False" );
</script>
|
Time Complexity: O(N), N is the size of the word array.
Space Complexity: O(M) where M is the length of str
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...