题解 | #牛群名字编码#
牛群名字编码
https://www.nowcoder.com/practice/9e86301a87954987932f3c7869b5a610
# # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param names string字符串一维数组 # @return int整型 # class Solution: def minimum_encoding_length(self, names: List[str]) -> int: # write code here indices = [name + "#" for name in names] indices.sort(key=lambda x: len(x)) indices = indices + ["#"] count = 0 for i in range(len(indices) - 1): for j in range(i + 1, len(indices)): if indices[j].endswith(indices[i]): break if j == len(indices) - 1: count += len(indices[i]) return count