Check if given String is prefix subarray of the given Array Last Updated : 12 May, 2022 Comments Improve Suggest changes Like Article Like Report 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: trueExplanation: 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: falseExplanation: 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++ // C++ program to implement the // given approach #include <bits/stdc++.h> using namespace std; // Function to check whether string // is prefix bool isPrefixString(string s, vector<string>& word) { // ans is taken as an empty string string ans = ""; // N is used to store // the size of word array int N = word.size(); // Iterating over the word array for (int i = 0; i < N; i++) { // Adding element by element // of the array ans += word[i]; // If ans and str are same // return true if (ans == s) return true; } // As iteration is ending which means // string is not prefix so return false. return false; } // Driver code 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 // Java program for the above approach import java.io.*; import java.lang.*; import java.util.*; class GFG { // Function to check whether string // is prefix static Boolean isPrefixString(String s, String word[]) { // ans is taken as an empty string String ans = ""; // N is used to store // the size of word array int N = word.length; // Iterating over the word array for (int i = 0; i < N; i++) { // Adding element by element // of the array ans += word[i]; // If ans and str are same // return true if (ans.equals(s)) return true; } // As iteration is ending which means // string is not prefix so return false. return false; } // Driver code 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"); } } // This code is contributed by hrithikgarg03188. Python # Pyhton program to implement the # given approach # Function to check whether string # is prefix def isPrefixString(s, word): # ans is taken as an empty string ans = "" # N is used to store # the size of word array N = len(word) # Iterating over the word array for i in range(0, N): # Adding element by element # of the array ans = ans + (word[i]) # If ans and str are same # return true if (ans == s): return True # As iteration is ending which means # string is not prefix so return false. return False # Driver code str = "indiaismycountry" word = ["india", "is", "my", "country", "and", "i", "love", "india"] ans = isPrefixString(str, word) if (ans == True): print("True") else: print("False") # This code is contributed by Samim Hossain Mondal. C# // C# program for the above approach using System; class GFG { // Function to check whether string // is prefix static bool isPrefixString(string s, string []word) { // ans is taken as an empty string string ans = ""; // N is used to store // the size of word array int N = word.Length; // Iterating over the word array for (int i = 0; i < N; i++) { // Adding element by element // of the array ans += word[i]; // If ans and str are same // return true if (ans.Equals(s)) return true; } // As iteration is ending which means // string is not prefix so return false. return false; } // Driver code 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"); } } // This code is contributed by Samim Hossain Mondal. JavaScript <script> // JavaScript code for the above approach // Function to check whether string // is prefix function isPrefixString(s, word) { // ans is taken as an empty string let ans = ""; // N is used to store // the size of word array let N = word.length; // Iterating over the word array for (let i = 0; i < N; i++) { // Adding element by element // of the array ans += word[i]; // If ans and str are same // return true if (ans == s) return true; } // As iteration is ending which means // string is not prefix so return false. return false; } // Driver code 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"); // This code is contributed by Potta Lokesh </script> OutputTrue Time Complexity: O(N), N is the size of the word array.Space Complexity: O(M) where M is the length of str Comment More infoAdvertise with us Next Article Check if given String is prefix subarray of the given Array rexomkar Follow Improve Article Tags : Strings Greedy DSA Arrays prefix Suffix +2 More Practice Tags : ArraysGreedyStrings Similar Reads Check if Prefix String exists in the Array Given an array of strings, the task is to print "Yes" if it contains a string that is a prefix of another string otherwise, print "No". Examples: Input: arr[] = {"rud", "rudra", "rahi"}Output: YesExplanation: arr[0] = "rud" is a prefix of arr[1] = "rudra", that's why "Yes" is the output. Input: arr[ 9 min read Length of all prefixes that are also the suffixes of given string Given a string S consisting of N characters, the task is to find the length of all prefixes of the given string S that are also suffixes of the same string S. Examples: Input: S = "ababababab"Output: 2 4 6 8Explanation: The prefixes of S that are also its suffixes are: "ab" of length = 2"abab" of le 10 min read Find the Suffix Array of given String with no repeating character Given a string str of size N, the task is to find the suffix array of the given string. Note: A suffix array is a sorted array of all suffixes of a given string. Examples: Input: str = "prince"Output: 4 5 2 3 0 1Explanation: The suffixes are0 prince 4 ce1 rince Sort the suffixes 5 e 2 ince --------- 6 min read Print all strings in the given array that occur as the substring in the given string 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: forggeekeksforExplanation: The strings "forg", "geek", "ek" and "sfor" occur as 5 min read Longest string in an array which matches with prefix of the given string Given an array of strings arr[] and Q queries where each query consists of a string str, the task is to find the longest string in the array that matches with prefix of the given string str i.e. the string must be prefix of str. Examples: Input: arr[] = {"GeeksForGeeks", "GeeksForGeeksd", "Arnab", " 8 min read Check whether the given string is a valid identifier Given a string str, the task is to check if the string is a valid identifier or not. In order to qualify as a valid identifier, the string must satisfy the following conditions: It must start with an either underscore(_) or any of the characters from the ranges ['a', 'z'] and ['A', 'Z'].There must n 6 min read Check if given words are present in a string Given a big string and an array of small strings, all of which are smaller in length than the big string. The task is to create an array of booleans, where each boolean represents whether the small string at that index in the array of small strings is contained in the big string. Note : that you can 15+ min read Count of Strings of Array having given prefixes for Q query Given two arrays of strings containing words[] and queries[] having N and Q strings respectively, the task is to find the number of strings from words[] having queries[i] as the prefix for all the strings in queries[]. Examples: Input: words[] = { "geeks", "geeks", "geeks for geeks", "string", "stro 13 min read Check if given string is a substring of string formed by repeated concatenation of z to a Given a string str, the task is to check if string str is a substring of an infinite length string S in which lowercase alphabets are concatenated in reverse order as: S = "zyxwvutsrqponmlkjihgfedcbazyxwvutsrqponmlkjihgfedcba...." Examples: Input: str = "cbaz"Output: YES Explanation:Given string "cb 10 min read Count of Superstrings in a given array of strings Given 2 array of strings X and Y, the task is to find the number of superstrings in X. A string s is said to be a Superstring, if each string present in array Y is a subsequence of string s . Examples: Input: X = {"ceo", "alco", "caaeio", "ceai"}, Y = {"ec", "oc", "ceo"}Output: 2Explanation: Strings 8 min read Like