Given range of strings arr<>, the task is to uncover the biggest string in the range which is made up of the various other strings from the array after concatenating one after ~ another. If no together string exists then print -1.
You are watching: Find longest word made of other words
Examples:Input: arr<> = “geeks”, “for”, “geeksfor”, “dearteassociazione.org”Output: dearteassociazione.org“dearteassociazione.org” is consisted of of (“geeks” + “for” + “geeks”).Even though “geeksfor” is also made up of various other stringsbut that is no the biggest string.Input: arr<> = “Hey”, “you”, “stop”, “right”, “there”Output : -1
Approach:Sort every the strings based on their lengths in decreasing order.Now, beginning from the longest string. Check for all feasible prefix that the string even if it is it is existing in the provided array and also for the remaining component of the string, recursively inspect whether it can be comprised from other strings from the array.Map have the right to be provided to inspect whether a string exist in the selection or not. The first string i beg your pardon satisfies the over conditions is the answer.If no such string exists then publish -1.Below is the implementation the the over approach:
Time complexity: O(N^3)Auxiliary Space: O(N). Fist reader! Don’t stop discovering now. Obtain hold of all the necessary DSA concepts with the DSA self Paced Course in ~ a student-friendly price and also become market ready. To finish your preparation from learning a language come DS Algo and also many more, you re welcome refer Complete Interview preparation Course.
See more: What Is The Max Number Of Breakers In A Panel ? How Many Breakers Can An Electrical Panel Have
In instance you wish to to visit live class with experts, you re welcome refer DSA Live Classes for Working specialists and Competitive Programming Live because that Students.
Stack Data framework (Introduction and Program)Multidimensional Arrays in JavaPython | utilizing 2D arrays/lists the ideal wayLinear SearchLinked perform vs Array
Header documents in C/C++ and also its usesProgram to print ASCII worth of a characterC++ regime to check if a offered String is Palindrome or notcin in C++How to return multiple values from a role in C or C++?