← Home

1585. Check If String Is Transformable With Substring Sort Operations

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
straightforward implementationC++Markdown
158

The trick here is to name the state correctly, then let the implementation follow. For 1585. Check If String Is Transformable With Substring Sort Operations, the solution in this repository is mainly a straightforward implementation solution.

Guide

What?

We want to turn the problem statement into a smaller set of decisions the computer can repeat safely. Instead of trying to be clever immediately, read the code as a sequence of questions:

  • What state are we keeping?
  • How do we move from one state to the next?
  • When do we know the answer is already determined?

For this file, the main tools are: straightforward implementation.

The notes already sitting in the source point us in the right direction:

  • https://leetcode.com/problems/check-if-string-is-transformable-with-substring-sort-operations/discuss/843917/C%2B%2BJavaPython-O(n)

Guide

When?

This pattern shows up when the brute force version has too many repeated checks, too many possible branches, or too much bookkeeping to do by hand. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are isTransformable, cnt.

Guide

Why?

The solution works because it narrows the problem until every update has a clear reason to exist.

  • The two-dimensional vector is the memory of the solution: grid state, DP state, or adjacency shape.
  • The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.

Guide

How?

Walk through the solution in this order:

  1. Read the setup variables first.
  2. Follow the main loop or recursive helper next.
  3. Watch where invalid states get skipped.
  4. Check which value survives to the return statement.

The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.

Guide

Complexity

  • Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
  • Space: O(n) in the usual case for auxiliary containers or recursion

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01//https://leetcode.com/problems/check-if-string-is-transformable-with-substring-sort-operations/discuss/843917/C%2B%2BJavaPython-O(n)
02//Runtime: 140 ms, faster than 67.24% of C++ online submissions for Check If String Is Transformable With Substring Sort Operations.
03//Memory Usage: 22.8 MB, less than 29.02% of C++ online submissions for Check If String Is Transformable With Substring Sort Operations.
04class Solution {
05public:
06    bool isTransformable(string s, string t) {
07        vector<vector<int>> pos(10);
08        
09        for(int i = 0; i < s.size(); ++i){
10            pos[s[i]-'0'].push_back(i);
11        }
12        
13        //count of processed digit
14        vector<int> cnt(10);
15        for(int i = 0; i < t.size(); ++i){
16            /*
17            in each iteration, 
18            we check if we can find an unprocessed d in s,
19            and "swap" it to t[i]
20            */
21            int d = t[i]-'0';
22            
23            if(cnt[d]+1 > pos[d].size()){
24                //cnt[d]+1: this is the (cnt[d]+1)th d  in t we meet
25                //pos[d].size(): we have pos[d].size() d in s
26                //this means we have more d in t than in s
27                //so we cannot find the unprocessed d in s
28                return false;
29            }
30            
31            for(int sd = 0; sd < d; ++sd){
32                //sd: any number smaller than d
33                if(cnt[sd] < pos[sd].size() && pos[sd][cnt[sd]] < pos[d][cnt[d]]){
34                    //cnt[sd] < pos[sd].size(): sd is not fully processed
35                    /*
36                    pos[sd][cnt[sd]] < pos[d][cnt[d]]: 
37                    some unprocessed digit is former than this unprocessed d (in s),
38                    that means we cannot swap sd and d,
39                    so we cannot put d into its right position
40                    */
41                    return false;
42                }
43            }
44            
45            //mark it as processed
46            ++cnt[d];
47        }
48        
49        return true;
50    }
51};

Cost

Complexity

Time
O(n) to O(n log n), depending on the dominant loop or data structure operation
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(n) in the usual case for auxiliary containers or recursion
Auxiliary state plus the answer structure where the problem requires one.