Skip to content

Commit c805826

Browse files
add 820
1 parent ec5a38e commit c805826

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,8 @@ _If you like this project, please leave me a star._ ★
421421
|832|[Flipping an Image](https://leetcode.com/problems/flipping-an-image/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_832.java) | |Easy|
422422
|830|[Positions of Large Groups](https://leetcode.com/problems/positions-of-large-groups/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_830.java) | |Easy|
423423
|824|[Goat Latin](https://leetcode.com/problems/goat-latin/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_824.java) | |Easy|
424-
|821|[Shortest Distance to a Character](https://leetcode.com/problems/shortest-distance-to-a-character/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_821.java) | |Easy|
424+
|821|[Shortest Distance to a Character](https://leetcode.com/problems/shortest-distance-to-a-character/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_821.java) | |Easy|
425+
|820|[Short Encoding of Words](https://leetcode.com/problems/short-encoding-of-words/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_820.java) | |Medium|
425426
|819|[Most Common Word](https://leetcode.com/problems/most-common-word/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_819.java) | |Easy| HashMap
426427
|814|[Binary Tree Pruning](https://leetcode.com/problems/binary-tree-pruning/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_814.java) | |Medium| recursion, DFS
427428
|811|[Subdomain Visit Count](https://leetcode.com/problems/subdomain-visit-count/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_811.java) | |Easy| HashMap
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.Arrays;
4+
5+
public class _820 {
6+
public static class Solution1 {
7+
public int minimumLengthEncoding(String[] words) {
8+
Arrays.sort(words, (a, b) -> a.length() - b.length());
9+
boolean[] removed = new boolean[words.length];
10+
for (int j = words.length - 2; j >= 0; j--) {
11+
for (int i = j + 1; i < words.length; i++) {
12+
if (!removed[i]) {
13+
if (words[i].substring(words[i].length() - words[j].length()).equals(words[j])) {
14+
removed[j] = true;
15+
break;
16+
}
17+
}
18+
}
19+
}
20+
int len = 0;
21+
for (int i = 0; i < words.length; i++) {
22+
if (!removed[i]) {
23+
len += words[i].length();
24+
len++;
25+
}
26+
}
27+
return len;
28+
}
29+
}
30+
}

0 commit comments

Comments
 (0)