
- Python 3 Basic Tutorial
- Python 3 - Home
- What is New in Python 3
- Python 3 - Overview
- Python 3 - Environment Setup
- Python 3 - Basic Syntax
- Python 3 - Variable Types
- Python 3 - Basic Operators
- Python 3 - Decision Making
- Python 3 - Loops
- Python 3 - Numbers
- Python 3 - Strings
- Python 3 - Lists
- Python 3 - Tuples
- Python 3 - Dictionary
- Python 3 - Date & Time
- Python 3 - Functions
- Python 3 - Modules
- Python 3 - Files I/O
- Python 3 - Exceptions
Program to check whether we can convert string in K moves or not using Python
Suppose we have two strings s and t, we have to check whether s can be converted to t in k moves or less. In ith move you can do these operations.
Select any index j (starting from 1) in s, such that 1 <= j <= size of s and j has not been selected in any previous move, and shift the character at that index i number of times.
Remain as it is.
Here shifting a character means replacing it by the next letter in the alphabet (if letter is 'z', then wrap it to 'a'). So shifting a character by i times indicates applying the shift operations i times.
So, if the input is like s = "poput" t = "vwput" k = 9, then the output will be True because at i = 6, we can convert 'p' to 'v', and at i = 8, we can convert 'o' to 'w'.
To solve this, we will follow these steps:
if size of s is not same as size of t, then
return False
count := an array holding (minimum of 1 and (k - i + 1 +(k - i)/26)) for all i from 0 to 25
for each character c1 from s and c2 from t, do
if c1 is not same as c2, then
diff :=(ASCII of c2 - ASCII of c1 + 26) mod 26
if count[diff] <= 0, then
return False
count[diff] := count[diff] - 1
return True
Let us see the following implementation to get better understanding
Example
def solve(s, t, k): if len(s) != len(t): return False count = [min(1, k - i + 1) + (k - i)//26 for i in range(26)] for c1, c2 in zip(s, t): if (c1 != c2): diff = (ord(c2) - ord(c1) + 26) % 26 if count[diff] <= 0: return False count[diff] -= 1 return True s = "poput" t = "vwput" k = 9 print(solve(s, t,k))
Input
"poput","vwput",9
Output
True
- Related Articles
- Program to check whether we can make k palindromes from given string characters or not in Python?
- Python program to check whether we can pile up cubes or not
- Program to check whether we can take all courses or not in Python
- Program to check whether we can unlock all rooms or not in python
- Program to check whether one string swap can make strings equal or not using Python
- Program to check whether we can get N queens solution or not in Python
- Program to check whether final string can be formed using other two strings or not in Python
- Python Program to Check Whether a String is a Palindrome or not Using Recursion
- Program to check whether we can split list into consecutive increasing sublists or not in Python
- Program to check whether palindrome can be formed after deleting at most k characters or not in python
- Python program to check whether a given string is Heterogram or not
- Program to check whether two string arrays are equivalent or not in Python
- How to Check Whether a String is Palindrome or Not using Python?
- Program to check whether Amal can win stone game or not in Python
- Program to check whether all can get a seat or not in Python
