Program to perform prefix compression from two strings in Python


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 −

  • lcp := blank string
  • for i in range 0 to minimum of size of s or size of t, do
    • if s[i] is same as t[i], then
      • lcp := lcp + s[i]
  • s_rem := substring of s from index (size of lcp) to end
  • t_rem := substring of t from index (size of lcp) to end
  • return a list of three pairs [(size of lcp , lcp) ,(size of s_rem , s_rem) ,(size of t_rem , t_rem)]

Example

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))

Input

"science", "school"

Output

[(2, 'sc'), (5, 'ience'), (4, 'hool')]

Updated on: 12-Oct-2021

208 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements