forked from Sunchit/Coding-Decoded
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathShortEncodingofWords.java
More file actions
37 lines (26 loc) · 823 Bytes
/
ShortEncodingofWords.java
File metadata and controls
37 lines (26 loc) · 823 Bytes
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
28
29
30
31
32
33
34
35
36
37
class Solution {
//O(n2)
// SC: O(n) , n of elements in words
public int minimumLengthEncoding(String[] words) {
// a b
Set<String> wordSet = new HashSet<>(Arrays.asList(words));
List<String> wordList = new ArrayList<>(wordSet);
Set<String> dupList = new HashSet<>();
for(int i=0;i<wordList.size();i++){
for(int j=0;j<wordList.size();j++){
if(i!=j){
if(wordList.get(i).endsWith(wordList.get(j))){
dupList.add(wordList.get(j));
}
}
}
}
int count = 0;
for(String word: wordList){
if(!dupList.contains(word)){
count += word.length() +1;
}
}
return count;
}
}