
- 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 check whether string Is transformable with substring sort operations in Python
Suppose we have two numeric strings s and t, we want to transform from string s to t using the following operation any number of times: 1. Select a non-empty substring in s and sort it inplace so the characters are in ascending order. We have to check whether it is possible to transform string s into string t or not.
So, if the input is like s = "95643" t = "45963", then the output will be True because we can transform s into t using like "95643" -> "95463" -> "45963".
To solve this, we will follow these steps −
places := a map where default value type is list
for i in range size of s down to 0, do
key := s[i] as integer
insert i at the end of places[key]
for each e in t, do
key := e as integer
if places[key] is empty, then
return False
i := last element of places[key]
for j in range 0 to key - 1, do
if places[j] is non-empty and last element of places[j] < i, then
return False
delete last element from places[key]
return True
Example
Let us see the following implementation to get better understanding
from collections import defaultdict def solve(s, t): places = defaultdict(list) for i in reversed(range(len(s))): key = int(s[i]) places[key].append(i) for e in t: key = int(e) if not places[key]: return False i = places[key][-1] for j in range(key): if places[j] and places[j][-1] < i: return False places[key].pop() return True s = "95643" t = "45963" print(solve(s, t))
Input
"95643", "45963"
Output
True
- Related Articles
- How to check if string or a substring of string starts with substring in Python?
- Python Program to check if a substring is present in a given string.
- Python Program to check if a string starts with a substring using regex
- How to check whether a string contains a substring in JavaScript?
- How to check whether a string contains a substring in jQuery?
- Python program to check whether the string is Symmetrical or Palindrome
- Program to check whether String Halves Are Alike in Python
- Golang Program to check a string starts with a specified substring
- Python program to sort strings by substring range
- Program to find minimum number of operations required to make one string substring of other in Python
- How to check if string or a substring of string ends with suffix in Python?
- Python program to check whether a given string is Heterogram or not
- How to check whether a String contains a substring or not?
- Check if substring present in string in Python
- How to check whether a string starts with XYZ in Python?
