
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
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]
- if s[i] is same as t[i], then
- 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')]
- Related Articles
- Program to perform string compression in Python
- Program to find longest common prefix from list of strings in Python
- Python Program to print strings based on the list of prefix
- Python program to find uncommon words from two Strings
- Python – Split Strings on Prefix Occurrence
- Program to create a lexically minimal string from two strings in python
- Python program to delete prefix substring from the given string
- Program to merge two strings in alternating fashion in Python
- Program to add two numbers represented as strings in Python
- Program to determine if two strings are close in Python
- Program to find largest merge of two strings in Python
- Python Program to compare two strings by ignoring case
- Length of longest prefix anagram which are common in given two strings
- Program to split two strings to make palindrome using Python
- Python program to remove words that are common in two Strings

Advertisements