File tree 2 files changed +39
-0
lines changed
2 files changed +39
-0
lines changed Original file line number Diff line number Diff line change 628
628
817|[ Linked List Components] ( ./0817-linked-list-components.js ) |Medium|
629
629
818|[ Race Car] ( ./0818-race-car.js ) |Hard|
630
630
819|[ Most Common Word] ( ./0819-most-common-word.js ) |Easy|
631
+ 820|[ Short Encoding of Words] ( ./0820-short-encoding-of-words.js ) |Medium|
631
632
821|[ Shortest Distance to a Character] ( ./0821-shortest-distance-to-a-character.js ) |Easy|
632
633
824|[ Goat Latin] ( ./0824-goat-latin.js ) |Easy|
633
634
827|[ Making A Large Island] ( ./0827-making-a-large-island.js ) |Hard|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 820. Short Encoding of Words
3
+ * https://leetcode.com/problems/short-encoding-of-words/
4
+ * Difficulty: Medium
5
+ *
6
+ * A valid encoding of an array of words is any reference string s and array of indices
7
+ * such that:
8
+ * - words.length == indices.length
9
+ * - The reference string s ends with the '#' character.
10
+ * - For each index indices[i], the substring of s starting from indices[i] and up to
11
+ * (but not including) the next '#' character is equal to words[i].
12
+ *
13
+ * Given an array of words, return the length of the shortest reference string s possible
14
+ * of any valid encoding of words.
15
+ */
16
+
17
+ /**
18
+ * @param {string[] } words
19
+ * @return {number }
20
+ */
21
+ var minimumLengthEncoding = function ( words ) {
22
+ const uniqueWords = [ ...new Set ( words ) ] ;
23
+ const wordSet = new Set ( uniqueWords ) ;
24
+
25
+ for ( const word of uniqueWords ) {
26
+ for ( let i = 1 ; i < word . length ; i ++ ) {
27
+ const suffix = word . slice ( i ) ;
28
+ wordSet . delete ( suffix ) ;
29
+ }
30
+ }
31
+
32
+ let result = 0 ;
33
+ for ( const word of wordSet ) {
34
+ result += word . length + 1 ;
35
+ }
36
+
37
+ return result ;
38
+ } ;
You can’t perform that action at this time.
0 commit comments