-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy path0893-groups-of-special-equivalent-strings.js
40 lines (38 loc) · 1.43 KB
/
0893-groups-of-special-equivalent-strings.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
/**
* 893. Groups of Special-Equivalent Strings
* https://leetcode.com/problems/groups-of-special-equivalent-strings/
* Difficulty: Medium
*
* You are given an array of strings of the same length words.
*
* In one move, you can swap any two even indexed characters or any two odd indexed characters of
* a string words[i].
*
* Two strings words[i] and words[j] are special-equivalent if after any number of moves,
* words[i] == words[j].
* - For example, words[i] = "zzxy" and words[j] = "xyzz" are special-equivalent because we may
* make the moves "zzxy" -> "xzzy" -> "xyzz".
*
* A group of special-equivalent strings from words is a non-empty subset of words such that:
* - Every pair of strings in the group are special equivalent, and
* - The group is the largest size possible (i.e., there is not a string words[i] not in the
* group such that words[i] is special-equivalent to every string in the group).
*
* Return the number of groups of special-equivalent strings from words.
*/
/**
* @param {string[]} words
* @return {number}
*/
var numSpecialEquivGroups = function(words) {
return new Set(words.map(normalizeWord)).size;
function normalizeWord(word) {
const evenChars = [];
const oddChars = [];
for (let i = 0; i < word.length; i++) {
if (i % 2 === 0) evenChars.push(word[i]);
else oddChars.push(word[i]);
}
return `${evenChars.sort().join('')}-${oddChars.sort().join('')}`;
}
};