Can Convert String in K Moves - Complete Solution Guide
Can Convert String in K Moves is LeetCode problem 1540, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
Given two strings s and t , your goal is to convert s into t in k moves or less. During the i th ( 1 <= i <= k ) move 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. Remembe
Detailed Explanation
The problem asks whether string `s` can be converted to string `t` in at most `k` moves. Each move allows shifting a character in `s` by a certain number of times (1 to k) to match the character in `t` at the same index. Shifting wraps around the alphabet ('z' becomes 'a'). Each index can be shifted only once. The inputs are the two strings `s` and `t` and the maximum number of moves `k`. The output is a boolean indicating whether the conversion is possible.
Solution Approach
The solution calculates the required shift difference for each corresponding character in `s` and `t`. It then counts how many times each shift difference (1-25) is needed. Finally, it iterates through each possible shift difference and checks if the maximum move needed for that shift difference and its duplicates exceeds `k`. If any exceedance is found, it returns `false`. Otherwise, returns `true`.
Step-by-Step Algorithm
- Step 1: Check if the lengths of `s` and `t` are equal. If not, return `false` because conversion is impossible.
- Step 2: Initialize an array `counts` of size 26 to store the frequency of each shift difference (1 to 25).
- Step 3: Iterate through the strings `s` and `t`. For each index `i`, calculate the shift difference between `t[i]` and `s[i]` using `(ord(t[i]) - ord(s[i]) + 26) % 26`.
- Step 4: If the shift difference is greater than 0, increment the corresponding count in the `counts` array.
- Step 5: Iterate through the `counts` array from shift 1 to 25. For each shift value, check if its occurrences can be handled within `k` moves. The maximum number of moves needed is `shift + (count - 1) * 26`. For example, if a shift value of `1` occurs 3 times. The moves needed could be 1, 27 and 53.
- Step 6: If the `max_move_needed` is greater than `k` for any shift value, return `false`.
- Step 7: If all shift values can be handled within `k` moves, return `true`.
Key Insights
- Insight 1: The core idea is to calculate the shift difference for each corresponding character pair in `s` and `t`. If the shift difference is greater than 0, it represents the number of moves needed to convert the character.
- Insight 2: The same shift difference can appear multiple times. We need to ensure that we have enough moves `k` to perform each of these shifts, taking into account that each move number can only be used once.
- Insight 3: Optimizing the shift numbers required. Since each move number can be used once, we need to maximize the total number of available move numbers. For a shift count `x`, we can also use `x + 26`, `x + 52` and so on, up to `k`.
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(1)
Topics
This problem involves: Hash Table, String.
Companies
Asked at: Infosys.