class Solution {
public int minimumLengthEncoding(String[] words) {
StringBuffer sb_array = new StringBuffer();
String[] str_array = new String[words.length];
for(int i = 0; i < words.length; ++i){
sb_array = new StringBuffer(words[i]);
sb_array = sb_array.reverse();
str_array[i] = sb_array.toString();
}
Arrays.sort(str_array);
words = str_array;
List<String> mem = new ArrayList<>();
mem.add(words[0]);
for(int i = 1; i < words.length; ++i){
String last_str = mem.get(mem.size() - 1);
int temp = fun(last_str, words[i]);
if(temp == -2){
mem.add(words[i]);
}else if(temp == -1){
mem.set(mem.size() - 1, words[i]);
}
}
int res = 0;
for(int i = 0; i < mem.size(); ++i){
res += mem.get(i).length();
}
res += mem.size();
return res;
}
public int fun(String a, String b){
int a_len = a.length();
int b_len = b.length();
int len = Math.min(a_len, b_len);
for(int i = 0; i < len; ++i){
if(a.charAt(i) != b.charAt(i)){
return -2;
}
}
if(a_len == b_len){
return 0;
}else if(a_len > b_len){
return 1;
}else{
return -1;
}
}
}
820. Short Encoding of Words
最新推荐文章于 2022-10-11 14:49:01 发布