VOOZH about

URL: https://www.geeksforgeeks.org/dsa/check-if-given-string-is-prefix-subarray-of-the-given-array/

⇱ Check if given String is prefix subarray of the given Array - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

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-

 
 


Output
True


 

Time Complexity: O(N), N is the size of the word array.
Space Complexity: O(M) where M is the length of str


 

Comment
Article Tags: