Given two strings s and t, transform string s into string t using the following operation any number of times:
s and sort it in place so the characters are in ascending order.
"14234" results in "12344".Return true if it is possible to transform s into t. Otherwise, return false.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "84532", t = "34852" Output: true Explanation: You can transform s into t using the following sort operations: "84532" (from index 2 to 3) -> "84352" "84352" (from index 0 to 2) -> "34852"
Example 2:
Input: s = "34521", t = "23415" Output: true Explanation: You can transform s into t using the following sort operations: "34521" -> "23451" "23451" -> "23415"
Example 3:
Input: s = "12345", t = "12435" Output: false
Constraints:
s.length == t.length1 <= s.length <= 105s and t consist of only digits.Problem Overview: You are given two strings s and t consisting of digits. You can repeatedly choose any substring of s and sort it in ascending order. The task is to determine whether these operations can transform s into t.
Approach 1: Greedy Algorithm with Position Tracking (O(n) time, O(n) space)
This approach processes t from left to right and greedily matches each digit with the earliest valid occurrence in s. Maintain queues (or lists) storing indices of each digit 0–9 in s. When you need digit d for the next position in t, pop the earliest index from the queue for d. The key constraint: no smaller digit can appear before this index in s that hasn’t been used yet. A smaller digit would block the transformation because sorting operations cannot move a larger digit ahead of a smaller one that appears earlier. Checking digits 0..d-1 ensures the move is valid. This greedy ordering directly models the restrictions imposed by substring sorting. It works in linear time because each index is processed once.
Approach 2: Index Matching with Character Buckets (O(n) time, O(n) space)
This method groups positions of digits in s into buckets indexed by digit value. While scanning t, you match each character with the next available index from its corresponding bucket. Before accepting that index, verify that no smaller digit bucket has an earlier unused index. If such a digit exists, the transformation would require moving a larger digit ahead of a smaller one, which substring sorting cannot achieve. Conceptually this approach is similar to the greedy solution but emphasizes bucket-based indexing and pointer tracking. The logic focuses on validating ordering constraints across digit groups rather than directly simulating operations.
Both approaches rely on understanding how sorting a substring affects relative order. Sorting can move smaller digits left but cannot move larger digits ahead of smaller ones already positioned earlier. This constraint turns the problem into verifying whether the relative ordering of digits in s can satisfy the target sequence in t. Efficient tracking of indices with queues or buckets avoids repeated scans.
Recommended for interviews: The greedy position-tracking solution is what most interviewers expect. It demonstrates a strong understanding of ordering constraints and uses efficient data structures from string processing and greedy strategy. A brute-force simulation of substring sorting would be far too slow, but discussing why it fails helps show problem intuition before presenting the optimal linear solution.
This approach leverages a greedy algorithm while tracking positions of each digit to determine if it's feasible to reorder s to match t. The idea is to attempt building the target string t in a greedy left-to-right fashion by sort operations on substrings of s.
This C solution uses arrays to track positions of digits in s and iteratively checks if forming t is possible. It employs greediness by iterating through t and validates if positions can traverse the required target through sorted substrings starting from the smallest numbers possible.
Time Complexity: O(n), Space Complexity: O(n) where n is the length of the string.
This method uses index matching along with character buckets to determine if s can be reconfigured to t. It uses frequency elements of characters to check if transformation pathways respect substring sort operations.
This C solution checks character counts for each index, ensuring that the count for all numbers up to the current digit matches between s and t at every stage, ensuring allowed order.
Time Complexity: O(n), Space Complexity: O(1) due to fixed-size counting arrays.
The problem is essentially equivalent to determining whether any substring of length 2 in string s can be swapped using bubble sort to obtain t.
Therefore, we use an array pos of length 10 to record the indices of each digit in string s, where pos[i] represents the list of indices where digit i appears, sorted in ascending order.
Next, we iterate through string t. For each character t[i] in t, we convert it to the digit x. We check if pos[x] is empty. If it is, it means that the digit in t does not exist in s, so we return false. Otherwise, to swap the character at the first index of pos[x] to index i, all indices of digits less than x must be greater than or equal to the first index of pos[x]. If this condition is not met, we return pos[x]false. Otherwise, we pop the first index from and continue iterating through string t.
After the iteration, we return true.
The time complexity is O(n times C), and the space complexity is O(n). Here, n is the length of string s, and C$ is the size of the digit set, which is 10 in this problem.
| Approach | Complexity |
|---|---|
| Greedy Algorithm with Position Tracking | Time Complexity: O(n), Space Complexity: O(n) where n is the length of the string. |
| Index Matching with Character Buckets | Time Complexity: O(n), Space Complexity: O(1) due to fixed-size counting arrays. |
| Bubble Sort | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Greedy Algorithm with Position Tracking | O(n) | O(n) | General case and most interview settings where you must validate ordering constraints efficiently |
| Index Matching with Character Buckets | O(n) | O(n) | When implementing digit buckets or queue-based index tracking for clearer separation by character |
花花酱 LeetCode 1585. Check If String Is Transformable With Substring Sort Operations - 刷题找工作 EP356 • Hua Hua • 1,502 views views
Watch 6 more video solutions →Practice Check If String Is Transformable With Substring Sort Operations with our built-in code editor and test cases.
Practice on FleetCode