Input: 'arr' = ["x", "xx", "y", "xyx"]
Output: 3
Explanation:
The longest possible string chain is “x” -> “xx” -> “xyx”.
The length of the given chain is 3, hence the answer is 3.
The first line of the input contains a single integer 'n' denoting the length of the 'arr'.
The next 'n' lines of the input contains elements of 'arr'.
Return the length of the longest possible string chain with strings chosen from the given array.
You don't need to print anything; it has already been taken care of. Just implement the function.
The total number of states are approximately equal to N ^ N. Note that state denotes a function call to ‘rec()’. In this approach, we generate all possible chains starting from ARR[I].
At the i’th position, we will explore the longest string chain which would be possible starting from this string(i’th). We will take the maximum of the length of all string chains possible from ‘i’ equal to 0 to ‘N’ - 1.
The recursion tree for ‘ARR’ = [“x”, “xx”, “y”, “xyx”] will be :
The total number of states are approximately equal to N ^ N. Note that state denotes a function call to ‘rec()’. In this approach, we generate all possible chains starting from ARR[I].
At the i’th position, we will explore the longest string chain which would be possible starting from this string(i’th). We will take the maximum of the length of all string chains possible from ‘i’ equal to 0 to ‘N’ - 1.
You can see in the recursion tree of approach 1 that there are many overlapping subproblems. We can use memoization here.
First, we sort ‘ARR’ according to the length of strings.
We start from the first string and iterate through all the characters of this string and check by removing any character if the string already exists in the map. If it exists then we take a maximum of all such cases. In the map, we store the string and length of the longest string chain ending at that string.
Let’s take ‘ARR’ = [“x”, “xx”, “y”, “xyx”]
Initialize ‘maxChain’ to 0.
Our first string is “x”. Initialize ‘curr’ for “x” to 1. We go through all characters and see by removing one by one. Here only 1 character is there so we will directly insert it into the map along with ‘curr’. Now ‘maxChain’ becomes 1.
Now our string is “xx”. Initialize ‘curr’ for “x” to 1. Now we see that by removing the first “x” we find that “x”(after removing) is already in the map, so we update ‘curr’ as max(curr, 1 + mp[“x”]). Hence ‘curr’ becomes 2. And we insert “xx” and 2 in the map. Now ‘maxChain’ becomes 2.
Now our string is “y”. Initialize ‘curr’ for “y” to 1. We go through all characters and see by removing one by one. Here only 1 character is there so we will directly insert it into the map along with ‘curr’. Now ‘maxChain’ remains 2.
Now our string is “xyx”. Initialize ‘curr’ for “xyx” to 1. Now we see that by removing “y” we find that “xx” is already in map, so we update ‘curr’ as max(curr, 1 + mp[“x”]).
Hence ‘curr’ becomes 3. And we insert “xyx” and 3 in the map. Now ‘maxChain’ becomes 3.
Hence our longest string chain is of length 3 and is [“x”, “xx”, “xyx”].