1153. String Transforms Into Another String
Input: str1 = "aabcc", str2 = "ccdee"
Output: true
Explanation: Convert 'c' to 'e' then 'b' to 'd' then 'a' to 'c'. Note that the order of conversions matter.Input: str1 = "leetcode", str2 = "codeleet"
Output: false
Explanation: There is no way to transform str1 to str2.bool canConvert(string str1, string str2) { // time: O(n); space: O(1)
if (str1.length() != str2.length()) return false;
if (str1 == str2) return true;
unordered_map<char, char> mp;
for (int i = 0; i < str1.length(); ++i) {
if (mp.count(str1[i]) && mp[str1[i]] != str2[i]) return false;
mp[str1[i]] = str2[i];
}
return unordered_set<char>(str2.begin(), str2.end()).size() < 26;
}Last updated