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
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
count[diff] := count[diff] - 1
Let us see the following implementation to get better understanding
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))