- 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 final string can be formed using other two strings or not in Python
Suppose we have two strings s, t, and another string r we have to check whether there is any way to get r by merging characters in order from s and t.
So, if the input is like s = "xyz" t = "mno" r = "xymnoz", then the output will be True, as xymnoz can be formed by interleaving xyz and mno.
To solve this, we will follow these steps −
Define a function solve() . This will take s, t, r
if s, t and r are empty, then
return True
if r is empty, then
return False
if s is empty, then
return true when t is same as r, otherwise false
if not t is non−zero, then
return s is same as r
if s[0] is same as r[0], then
if solve(s[from index 1 to end], t, r[from index 1 to end]) is true, then
return True
if t[0] is same as r[0], then
if solve(s, t[from index 1 to end], r[from index 1 to end]) is true, then
return True
return False
Let us see the following implementation to get better understanding −
Example
class Solution: def solve(self, s, t, r): if not s and not t and not r: return True if not r: return False if not s: return t == r if not t: return s == r if s[0] == r[0]: if self.solve(s[1:], t, r[1:]): return True if t[0] == r[0]: if self.solve(s, t[1:], r[1:]): return True return False ob = Solution() s = "xyz" t = "mno" r = "xymnoz" print(ob.solve(s, t, r))
Input
"xyz", "mno", "xymnoz"
Output
True
- Related Articles
- Program to check whether two trees can be formed by swapping nodes or not in Python
- Program to check whether one string swap can make strings equal or not using Python
- Program to check two strings can be equal by swapping characters or not in Python
- Program to check whether we can convert string in K moves or not using Python
- Program to check whether palindrome can be formed after deleting at most k characters or not in python
- Program to check whether two string arrays are equivalent or not in Python
- Java Program to check whether two Strings are an anagram or not.
- Program to check whether one point can be converted to another or not in Python
- Program to check whether first player can take more candies than other or not in Python
- Program to check strings are rotation of each other or not in Python
- Check whether two strings are equivalent or not according to given condition in Python
- Check whether second string can be formed from characters of first string in Python
- Python Program to Check Whether a String is a Palindrome or not Using Recursion
- Check whether given string can be generated after concatenating given strings in Python
- Program to check whether two sentences are similar or not in Python
