- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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

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

"science", "school"

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

- Related Questions & Answers
- Program to perform string compression in Python
- Program to find longest common prefix from list of strings in Python
- Python program to find uncommon words from two Strings
- Python Program to print strings based on the list of prefix
- Program to create a lexically minimal string from two strings in python
- Python – Split Strings on Prefix Occurrence
- 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
- Program to split two strings to make palindrome using Python
- Java Program to Compare Two Strings
- C++ Program to Concatenate Two Strings
- C program to swap two strings
- Python program to remove words that are common in two Strings

Advertisements