Skip to content

Commit ac0c6a3

Browse files
authored
Create 1153-string-transforms-into-another-string.js
1 parent 4d93cc0 commit ac0c6a3

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
3+
Given two strings str1 and str2 of the same length,
4+
determine whether you can transform str1 into str2 by doing zero or more conversions.
5+
In one conversion you can convert all occurrences of one character
6+
in str1 to any other lowercase English character.
7+
Return true if and only if you can transform str1 into str2.
8+
9+
Example 1:
10+
11+
Input: str1 = "aabcc", str2 = "ccdee"
12+
Output: true
13+
Explanation: Convert 'c' to 'e' then 'b' to 'd' then 'a' to 'c'.
14+
Note that the order of conversions matter.
15+
16+
Example 2:
17+
18+
Input: str1 = "leetcode", str2 = "codeleet"
19+
Output: false
20+
Explanation: There is no way to transform str1 to str2.
21+
22+
Note:
23+
24+
1 <= str1.length == str2.length <= 10^4
25+
Both str1 and str2 contain only lowercase English letters.
26+
27+
*/
28+
29+
/**
30+
* @param {string} str1
31+
* @param {string} str2
32+
* @return {boolean}
33+
*/
34+
const canConvert = function(str1, str2) {
35+
if (str1 === str2) return true
36+
const map = new Map()
37+
for (let i = 0; i < str1.length; i++) {
38+
if (map.has(str1[i]) && map.get(str1[i]) !== str2[i]) {
39+
return false
40+
}
41+
map.set(str1[i], str2[i])
42+
}
43+
const set = new Set(map.values())
44+
return set.size < 26
45+
}

0 commit comments

Comments
 (0)