Given an variety of strings arr<>, the job is to discover the largest string in the selection which is comprised of the various other strings from the array after concatenating one after another. If no such string exists then print -1.
You are watching: Find longest word made of other words
Examples:Input: arr<> = “geeks”, “for”, “geeksfor”, “jiyuushikan.org”Output: jiyuushikan.org“jiyuushikan.org” is consisted of of (“geeks” + “for” + “geeks”).Even though “geeksfor” is additionally made up of other stringshowever it is not the largest string.Input: arr<> = “Hey”, “you”, “stop”, “right”, “there”Output : -1
Approach:Sort all the strings based upon their lengths in decreasing order.Now, founding from the longest string. Check for all feasible predeal with of the string whether it is existing in the offered selection and also for the staying part of the string, recursively check whether it can be comprised from various other strings from the array.Map deserve to be used to check whether a string exists in the selection or not. The initially string which satisfies the over conditions is the answer.If no such string exists then print -1.Below is the implementation of the above approach:
Time complexity: O(N^3)Auxiliary Space: O(N). Attention reader! Don’t stop learning currently. Get host of all the essential DSA principles with the DSA Self Paced Course at a student-friendly price and come to be sector ready. To finish your preparation from discovering a language to DS Algo and many more, please refer Complete Intercheck out Preparation Course.
See more: Why Does My Dog Sleep In The Closet ? 5 Warning Signs Your Dog Is Depressed
In case you wish to attend live classes via specialists, please refer DSA Live Classes for Working Professionals and also Competitive Programming Live for Students.
Check whether 2 strings have the right to be made equal by reversing substring of equal length from both strings
Count N-size arrays made from initially M herbal numbers whose subarrays can be made palindromic by replacing less than fifty percent of its elements
Check if a string deserve to be made palindromic by swapping pairs of personalities from indices having actually unequal personalities in a Binary String
Check if all selection aspects can be made divisible by K by replacing variety aspects through sum of pairs
Check if sum of variety deserve to be made equal to X by removing either the first or last digits of eexceptionally selection element
Stack Documents Structure (Overview and also Program)Maximum and minimum of an selection using minimum variety of comparisonsLiclose to SearchGiven an range A<> and a number x, inspect for pair in A<> with sum as xK"th Smallest/Largest Element in Unsorted Array | Set 1
Program to print ASCII Value of a characterHeader records in C/C++ and its usesCheck if given number is perfect squareHow to return multiple values from a role in C or C++?Sorting a Map by worth in C++ STL