- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Program to check whether two sentences are similar or not in Python
Suppose we have two sentences s and t. We have to check whether they are similar or not. Here sentence has only English letters. Two sentences are said to be similar when it is possible to add an arbitrary sentence (possibly empty) inside one of these given sentences such that the two sentences become equal.
So, if the input is like s = "we live at city Kolkata" t = "city Kolkata", then the output will be True because we can get s from t by adding sentence "we live in".
To solve this, we will follow these steps −
s1 := a list of words in s
s2 := a list of words in t
if size of s1 > size of s2, then
swap s1 and s2
while s1 is not empty, do
if s2[0] is same as s1[0], then
delete first word from s2
delete first word from s1
otherwise when last word of s2 is same as last word of s1, then
delete last word from s2
delete last word from s1
otherwise,
return false
return true
Example
Let us see the following implementation to get better understanding −
def solve(s, t): s1 = s.split() s2 = t.split() if len(s1) > len(s2): s1,s2 = s2,s1 while(s1): if(s2[0]==s1[0]): s2.pop(0) s1.pop(0) elif(s2[-1]==s1[-1]): s2.pop() s1.pop() else: return(False) return(True) s = "we live at city Kolkata" t = "city Kolkata" print(solve(s, t))
Input
"we live at city Kolkata", "city Kolkata"
Output
True
- Related Articles
- Program to check whether two string arrays are equivalent or not in Python
- Program to check whether parentheses are balanced or not in Python
- Golang Program to Check Whether Two Matrices are Equal or Not
- Swift Program to Check Whether Two Matrices Are Equal or Not
- Program to check whether leaves sequences are same of two leaves or not in python
- Program to check whether elements frequencies are even or not in Python
- Java Program to check whether two Strings are an anagram or not.
- C# program to check whether two sequences are the same or not
- Check whether two strings are equivalent or not according to given condition in Python
- Program to check whether all leaves are at same level or not in Python
- Program to check whether domain and range are forming function or not in Python
- Program to check whether two trees can be formed by swapping nodes or not in Python
- Program to check whether different brackets are balanced and well-formed or not in Python
- Program to check whether all palindromic substrings are of odd length or not in Python
- Python program to check whether a list is empty or not?
