Skip to content

Commit 6a09bda

Browse files
committed
Add solution #820
1 parent ec49b85 commit 6a09bda

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,7 @@
628628
817|[Linked List Components](./0817-linked-list-components.js)|Medium|
629629
818|[Race Car](./0818-race-car.js)|Hard|
630630
819|[Most Common Word](./0819-most-common-word.js)|Easy|
631+
820|[Short Encoding of Words](./0820-short-encoding-of-words.js)|Medium|
631632
821|[Shortest Distance to a Character](./0821-shortest-distance-to-a-character.js)|Easy|
632633
824|[Goat Latin](./0824-goat-latin.js)|Easy|
633634
827|[Making A Large Island](./0827-making-a-large-island.js)|Hard|
+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
};

0 commit comments

Comments
 (0)