File tree Expand file tree Collapse file tree 2 files changed +41
-0
lines changed Expand file tree Collapse file tree 2 files changed +41
-0
lines changed Original file line number Diff line number Diff line change 703
703
890|[ Find and Replace Pattern] ( ./0890-find-and-replace-pattern.js ) |Medium|
704
704
891|[ Sum of Subsequence Widths] ( ./0891-sum-of-subsequence-widths.js ) |Hard|
705
705
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|
706
707
901|[ Online Stock Span] ( ./0901-online-stock-span.js ) |Medium|
707
708
905|[ Sort Array By Parity] ( ./0905-sort-array-by-parity.js ) |Easy|
708
709
909|[ Snakes and Ladders] ( ./0909-snakes-and-ladders.js ) |Medium|
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments