题解 | #牛群名字编码#

牛群名字编码

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

全部评论

相关推荐

05-09 12:23
已编辑
华南理工大学 Java
野猪不是猪🐗:给他装的,双九+有实习的能看的上这种厂我直接吃⑨✌们拿它练练面试愣是给他整出幻觉了
点赞 评论 收藏
分享
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-02 17:58
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务