Check If String Is Transformable With Substring Sort Operations
Given two strings s and t, you want to transform string s into string t using the following operation any number of times:
- Choose a non-empty substring in
sand sort it in-place so the characters are in ascending order.
For example, applying the operation on the underlined substring in "14234" results in "12344".
Return true if it is possible to transform string s into string 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
Example 4:
Input: s = "1", t = "2" Output: false
Constraints:
s.length == t.length1 <= s.length <= 105sandtonly contain digits from'0'to'9'.
class Solution {
public:
bool isTransformable(string s, string t) {
int n=t.size(),m=s.size();
int i=0,j=0;
if(n!=m)
return false;
for(char ch='9';ch>='0';--ch)
{
vector<int> ds,dt;
for(int i=0;i<s.size();++i)
{
if(s[i]==ch)ds.push_back(i);
if(t[i]==ch)dt.push_back(i);
}
if((int)ds.size()!=(int)dt.size())return false;
for(int i=0;i<ds.size();++i)
if(ds[i]>dt[i])return false;
string s1="",t1="";
for(auto c:s)
if(c<ch)s1.push_back(c);
for(auto c:t)
if(c<ch)t1.push_back(c);
s=s1,t=t1;
}
return true;
}
};
