-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day 47
27 lines (27 loc) · 897 Bytes
/
Day 47
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import java.util.*;
class Solution {
public int minimumLengthEncoding(String[] words) {
StringBuilder sb = new StringBuilder();
HashSet<String> set = new HashSet<>();
for(String word : words){
set.add(word);
}
// Collection<String> removedWords = new LinkedList<String>(); // 80% faster
ArrayList<String> al = new ArrayList<String>(); // 40% faster
for(String str : set){
for(int j=1; j<str.length(); j++){
if(set.contains(str.substring(j))){
// removedWords.add(str.substring(j));
al.add(str.substring(j));
}
}
}
// set.removeAll(removedWords);
set.removeAll(al);
for(String s : set){
sb.append(s+"#");
}
// System.out.println(sb);
return sb.length();
}
}