
- 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
Check if it is possible to transform one string to another in Python
Suppose we have two strings s and t, t is in uppercase. We have to check whether we can convert s to t by performing following operations.
- Convert some lowercase letters uppercase.
- Remove all of the lowercase letters.
So, if the input is like s = "fanToM", t = "TOM", then the output will be True as we can change ‘o’ to ‘O’ then remove all other lowercase letters from s to make it t.
To solve this, we will follow these steps −
- n := size of s, m := size of t
- dp:= a matrix of size (m + 1)x(n + 1) and fill with False
- dp[0, 0] := True
- for i in range 0 to size of s - 1, do
- for j in range 0 to size of t, do
- if dp[i, j] is True, then
- if j < size of t and s[i] in uppercase is same as t[j], then
- dp[i + 1, j + 1] := True
- if s[i] is not in uppercase, then
- dp[i + 1, j] := True
- if j < size of t and s[i] in uppercase is same as t[j], then
- if dp[i, j] is True, then
- for j in range 0 to size of t, do
- return dp[n, m]
Example
Let us see the following implementation to get better understanding −
def solve(s,t): n = len(s) m = len(t) dp= [[False for i in range(m+1)] for i in range(n+1)] dp[0][0] = True for i in range(len(s)): for j in range(len(t)+1): if dp[i][j] == True: if j < len(t) and (s[i].upper() == t[j]): dp[i + 1][j + 1] = True if s[i].isupper()==False: dp[i + 1][j] = True return dp[n][m] s = "fanToM" t = "TOM" print(solve(s, t))
Input
"fanToM", "TOM"
Output
True
- Related Articles
- Check if it is possible to convert one string into another with given constraints in Python
- Check if it is possible to create a palindrome string from given N in Python
- Check if it is possible to survive on Island in Python
- Is it possible to check if a String only contains ASCII in java?
- Check if it is possible to sort the array after rotating it in Python
- Check if it is possible to form string B from A under the given constraint in Python
- Check if it is possible to rearrange a binary string with alternate 0s and 1s in Python
- How to check if a substring is contained in another string in Python
- Check if a string is suffix of another in Python
- Check if it is possible to serve customer queue with different notes in Python
- Check if a string can be repeated to make another string in Python
- Program to check one string can be converted to another by removing one element in Python
- How to check if multiple strings exist in another string in Python?
- Print all possible ways to convert one string into another string in C++
- Add one Python string to another

Advertisements