Help in this problem

Revision en1, by algoprince, 2025-04-04 12:09:53

You are given an array of strings. You can merge two strings, arr[i] and arr[j], only if, i < j and the last letter of arr[i] == first letter of arr[j] Eg, you have two strings -> “123” and “389” -> after merging it becomes “123389” You can keep merging strings like this, but the ‘final’ string you form must be such that the first letter of the string should be the same as the last letter. Eg, after merging several strings, the ‘final’ string becomes -> “123389…………1 Print the max length of the ‘final’ string that can be formed in this way. Test case — 1: Array of strings = [“14”, “123”, “323”, “321”, “421”, “535”] Possible ‘final’ strings -> “323”, “535”, “14421”, “123323321”, “123321” Ans = 9 (which is the length of “123323321”) Test case – 2: Array of strings = [“14”, “15”, “89”, “22”] Possible ‘final’ strings -> “22” Ans = 2 (which is the length+ of “22”) 1 <= ai <=1e9. 1 <=N<= 1e5

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en1 English algoprince 2025-04-04 12:09:53 945 Initial revision (published)