-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy path1657-determine-if-two-strings-are-close.js
47 lines (42 loc) · 1.33 KB
/
1657-determine-if-two-strings-are-close.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/**
* 1657. Determine if Two Strings Are Close
* https://leetcode.com/problems/determine-if-two-strings-are-close/
* Difficulty: Medium
*
* Two strings are considered close if you can attain one from the other using the following
* operations:
*
* - Operation 1: Swap any two existing characters.
* For example, abcde -> aecdb
* - Operation 2: Transform every occurrence of one existing character into another existing
* character, and do the same with the other character.
* For example, aacabb -> bbcbaa (all a's turn into b's, and all b's turn into a's)
*
* You can use the operations on either string as many times as necessary.
*
* Given two strings, word1 and word2, return true if word1 and word2 are close, and false
* otherwise.
*/
/**
* @param {string} word1
* @param {string} word2
* @return {boolean}
*/
var closeStrings = function(word1, word2) {
const map1 = helper(word1);
const map2 = helper(word2);
for (let i = 0; i < map1.length; i++) {
if ((map1[i] === 0 || map2[i] === 0) && map1[i] !== map2[i]) {
return false;
}
}
[map1, map2].forEach(m => m.sort((a, b) => b - a));
return map1.join('') === map2.join('');
};
function helper(input) {
const map = Array(26).fill(0);
for (let i = 0; i < input.length; i++) {
map[input.charCodeAt(i) - 'a'.charCodeAt(0)]++;
}
return map;
}