免费设置前缀

Qui*_*ver 1 python algorithm prefixes prefix information-theory

是否有标准或最佳算法使一组给定的字符串无前缀?也就是说,给定一组字符串,抛弃在该集合中也具有(较短)前缀的所有字符串.

如果它很重要,我最终将在Python 2.7中实现它.

agh*_*ast 5

strings = ['a', 'apple', 'b', 'beta', 'c', 'd']

def prefices_only(strlist):
    ordered = sorted(strlist)
    last = ordered[0]
    results = [last]

    for c in ordered:
        if not c.startswith(last):
            last = c
            results.append(c)

    return results

print(prefices_only(strings))
Run Code Online (Sandbox Code Playgroud)