Ctrl K

Letter Shifts on a Timer

Medium
You are given strings source and target made of lowercase English letters, and a number moves. The goal is to turn source into target in at most moves moves. Moves are numbered from 1. On move number i you may do one of two things: - pick a position in source that has never been picked before and shift its letter forward in the alphabet by exactly i steps (after z comes a again); - skip the move. Each position can be picked at most once. Return True if source can be turned into target, and False otherwise. The strings may differ in length, in which case it is impossible.
Example 1
source="cat", target="dog", moves=20 → True

The shifts are c→d by 1, a→o by 14 and t→g by 13 (wrapping past z). They are all different and the largest is 14 ≤ 20, so moves 1, 13 and 14 do the job.

Example 2
source="aaa", target="bbb", moves=27 → False

Each letter needs a shift of 1. The first can use move 1, the second move 27 (a full lap plus one), but the third would need move 53 > 27.

Example 3
source="go", target="go", moves=0 → True

The strings already match, so no moves are needed.

Constraints
  • 1 ≤ len(source), len(target) ≤ 10⁵
  • 0 ≤ moves ≤ 10⁹
  • Both strings consist of lowercase English letters
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsLongest Repeat-Free Stretch MediumNumber in Roman Numerals MediumKeypad Words MediumAnagram Buckets MediumSplit Into Dictionary Words Medium
loading Python…
Input
source="cat", target="dog", moves=20
Expected
True

Run checks the first 3 tests in your browser. Submit checks all 122 tests on the server, hidden ones included.