#1540

Can Convert String in K Moves

expert · 1095 · lc medium +32 · verified · 37.1% accepted · 427 likes · top 15%

Description

You want to convert string s into string t in at most k moves. On move i (1-indexed), you may choose any index of s not previously used and cyclically shift that character forward by i positions (wrapping 'z' to 'a'), or do nothing. Each index can be acted on at most once. Return true if conversion is achievable, otherwise false.

Example 1:

Input: s = "input", t = "ouput", k = 9
Output: true
Explanation: In the 6th move, we shift 'i' 6 times to get 'o'. And in the 7th move we shift 'n' to get 'u'.

Example 2:

Input: s = "abc", t = "bcd", k = 10
Output: false
Explanation: We need to shift each character in s one time to convert it into t. We can shift 'a' to 'b' during the 1st move. However, there is no way to shift the other characters in the remaining moves to obtain t from s.

Example 3:

Input: s = "aab", t = "bbb", k = 27
Output: true
Explanation: In the 1st move, we shift the first 'a' 1 time to get 'b'. In the 27th move, we shift the second 'a' 27 times to get 'b'.

Code

1
2
3