Skip to content

Commit a4257c4

Browse files
committed
Add solution #893
1 parent e627daf commit a4257c4

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -703,6 +703,7 @@
703703
890|[Find and Replace Pattern](./0890-find-and-replace-pattern.js)|Medium|
704704
891|[Sum of Subsequence Widths](./0891-sum-of-subsequence-widths.js)|Hard|
705705
892|[Surface Area of 3D Shapes](./0892-surface-area-of-3d-shapes.js)|Easy|
706+
893|[Groups of Special-Equivalent Strings](./0893-groups-of-special-equivalent-strings.js)|Medium|
706707
901|[Online Stock Span](./0901-online-stock-span.js)|Medium|
707708
905|[Sort Array By Parity](./0905-sort-array-by-parity.js)|Easy|
708709
909|[Snakes and Ladders](./0909-snakes-and-ladders.js)|Medium|
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* 893. Groups of Special-Equivalent Strings
3+
* https://leetcode.com/problems/groups-of-special-equivalent-strings/
4+
* Difficulty: Medium
5+
*
6+
* You are given an array of strings of the same length words.
7+
*
8+
* In one move, you can swap any two even indexed characters or any two odd indexed characters of
9+
* a string words[i].
10+
*
11+
* Two strings words[i] and words[j] are special-equivalent if after any number of moves,
12+
* words[i] == words[j].
13+
* - For example, words[i] = "zzxy" and words[j] = "xyzz" are special-equivalent because we may
14+
* make the moves "zzxy" -> "xzzy" -> "xyzz".
15+
*
16+
* A group of special-equivalent strings from words is a non-empty subset of words such that:
17+
* - Every pair of strings in the group are special equivalent, and
18+
* - The group is the largest size possible (i.e., there is not a string words[i] not in the
19+
* group such that words[i] is special-equivalent to every string in the group).
20+
*
21+
* Return the number of groups of special-equivalent strings from words.
22+
*/
23+
24+
/**
25+
* @param {string[]} words
26+
* @return {number}
27+
*/
28+
var numSpecialEquivGroups = function(words) {
29+
return new Set(words.map(normalizeWord)).size;
30+
31+
function normalizeWord(word) {
32+
const evenChars = [];
33+
const oddChars = [];
34+
for (let i = 0; i < word.length; i++) {
35+
if (i % 2 === 0) evenChars.push(word[i]);
36+
else oddChars.push(word[i]);
37+
}
38+
return `${evenChars.sort().join('')}-${oddChars.sort().join('')}`;
39+
}
40+
};

0 commit comments

Comments
 (0)