Can Convert String in K Moves - Problem

Given two strings s and t, your goal is to convert s into t in k moves or less.

During the i-th move (1 <= i <= k) you can:

  • Choose any index j (1-indexed) from s, such that 1 <= j <= s.length and j has not been chosen in any previous move, and shift the character at that index i times.
  • Do nothing.

Shifting a character means replacing it by the next letter in the alphabet (wrapping around so that 'z' becomes 'a'). Shifting a character by i means applying the shift operations i times.

Remember that any index j can be picked at most once.

Return true if it's possible to convert s into t in no more than k moves, otherwise return false.

Input & Output

Example 1 — Basic Conversion
$ Input: s = "input", t = "ouput", k = 9
Output: true
💡 Note: We need to convert 'i'→'o' (shift 6), 'n'→'u' (shift 7), 'p'→'p' (shift 0, no change), 'u'→'u' (shift 0, no change), 't'→'t' (shift 0, no change). We need shifts of 6 and 7, which can be achieved with moves 6 and 7 respectively, both ≤ 9.
Example 2 — Impossible Case
$ Input: s = "abc", t = "bcd", k = 2
Output: false
💡 Note: We need 'a'→'b' (shift 1), 'b'→'c' (shift 1), 'c'→'d' (shift 1). All three positions need shift amount 1, but we only have moves 1 and 2. Move 1 creates shift 1, move 2 creates shift 2. We need 3 positions with shift 1 but only have 1 move that creates shift 1.
Example 3 — Wrap Around
$ Input: s = "ab", t = "bb", k = 1
Output: true
💡 Note: We need 'a'→'b' (shift 1), 'b'→'b' (no change). One position needs shift 1, and we have move 1 available which creates shift 1.

Constraints

  • 1 ≤ s.length, t.length ≤ 105
  • 0 ≤ k ≤ 109
  • s and t consist of lowercase English letters only

Visualization

Tap to expand
Can Convert String in K Moves INPUT String s: i n p u t String t: o u p u t k = 9 (max moves) s.length = t.length = 5 Each position needs shift Shifts needed: i-->o: 6, n-->u: 7 p-->p: 0, u-->u: 0 t-->t: 0 ALGORITHM STEPS 1 Calculate Shifts diff = (t[i] - s[i] + 26) % 26 2 Count Each Shift Track frequency of shifts 3 Assign Moves Greedy: d, d+26, d+52... 4 Check Max Move Verify max needed <= k Shift Frequency Table Shift Count Move 0 3 skip 6 1 6 7 1 7 FINAL RESULT Output: true Conversion is possible! Verification: Max move needed: 7 k = 9 7 <= 9 ... OK All shifts assignable Move Assignments: Position 0: Move 6 Position 1: Move 7 Position 2,3,4: No shift All within k=9 Key Insight: If we need the same shift d multiple times, we use moves d, d+26, d+52, etc. (cycling through alphabet). For n occurrences of shift d, the maximum move needed is d + 26*(n-1). We check if this exceeds k. Time: O(n) | Space: O(26) = O(1) for shift frequency array TutorialsPoint - Can Convert String in K Moves | Greedy Assignment Approach
Asked in
Google 15 Amazon 12
23.5K Views
Medium Frequency
~25 min Avg. Time
847 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen