Suppose we have two strings s and t (both contains lowercase English letters). We have to find a list of pairs of size 3, where each pair is in this form (l, k) here k is a string and l is its length. Now among these three pairs, first one contains substring of s and t which is longest common prefix p of these two strings, then the remaining part of s is s' and remaining part of t is t'. So final list will be like [(length of p, p), (length of s', s'), (length of t', t')].
So, if the input is like s = "science" t = "school", then the output will be [(2, 'sc'), (5, 'ience'), (4, 'hool')]
To solve this, we will follow these steps −
Let us see the following implementation to get better understanding −
def solve(s, t): lcp = '' for i in range(min(len(s), len(t))): if s[i] == t[i]: lcp += s[i] s_rem = s[len(lcp):] t_rem = t[len(lcp):] return [(len(lcp), lcp), (len(s_rem), s_rem), (len(t_rem), t_rem)] s = "science" t = "school" print(solve(s, t))
[(2, 'sc'), (5, 'ience'), (4, 'hool')]