You are given two strings s and t made of lowercase English letters, and an integer k. You want to turn s into t in at most k moves. The moves are numbered 1, 2, ..., k, and during move i you may do one of two things:
-
Pick an index of
s
that no earlier move has picked, and shift the character at that index
i
times.
-
Do nothing.
Shifting a character once replaces it with the next letter of the alphabet, wrapping around so that 'z' becomes 'a'. Shifting it i times applies that operation i times. Each index can be picked at most once.
Return True if s can be turned into t in no more than k moves, otherwise return False.
Function Signature
def can_convert_string(s: str, t: str, k: int) -> bool:
Rules
-
Moves are numbered from 1, and move
i
shifts the chosen character forward by exactly
i
letters. Because the alphabet wraps, a shift of 27 has the same effect as a shift of 1.
-
A move changes at most one index, and an index can be changed by at most one move.
-
An index where
s
and
t
already hold the same letter needs no move.
-
If
s
and
t
have different lengths, return
False
.
Constraints
-
1 <= len(s), len(t) <= 10^5
-
0 <= k <= 10^9
-
s
and
t
contain only lowercase English letters.
Examples
Input: s = "input", t = "ouput", k = 9
Output: True
Turning 'i' into 'o' takes 6 shifts, so it uses move 6. Turning 'n' into 'u' takes 7 shifts, so it uses move 7. The last three letters already match, and both moves are within 9.
Input: s = "abc", t = "bcd", k = 10
Output: False
All three positions need a shift of 1. The only moves that turn a letter into the next one are 1, 27, 53 and so on, so the second and third positions would need moves 27 and 53, which are past 10.
Input: s = "aab", t = "bbb", k = 27
Output: True
The first two positions each need a shift of 1, and moves 1 and 27 cover them. The last letter already matches. With k = 26 the answer would be False.