题目

Recover a secret string from random triplets

从随机三元组中恢复秘密字符串

描述

There is a secret string which is unknown to you. Given a collection of random triplets from the string, recover the original string.

A triplet here is defined as a sequence of three letters such that each letter occurs somewhere before the next in the given string. "whi" is a triplet for the string "whatisup".

As a simplification, you may assume that no letter occurs more than once in the secret string.

You can assume nothing about the triplets given to you other than that they are valid triplets and that they contain sufficient information to deduce the original string. In particular, this means that the secret string will never contain letters that do not occur in one of the triplets given to you.

有一个你不知道的秘密字符串。给定字符串中的随机三元组集合,恢复原始字符串。

这里的三元组定义为三个字母的序列,每个字母出现在给定字符串中下一个字母之前的某个位置。“whi”是字符串“whatisup”的三元组。

作为一种简化,您可以假设机密字符串中没有字母出现超过一次。

对于提供给您的三元组,您只能假设它们是有效的三元组,并且它们包含足够的信息来推断原始字符串。特别是,这意味着秘密字符串永远不会包含在给定给您的三元组中不出现的字母。

例子

secret = "whatisup"
triplets = [
  ['t','u','p'],
  ['w','h','i'],
  ['t','s','u'],
  ['a','t','s'],
  ['h','a','p'],
  ['t','i','s'],
  ['w','h','s']
]

思路

不知道大家看完上面的描述理解了没有,反正我是没有立马理解的,哈哈

这题的意思呢,就是通过triplets这个三元组的排列顺序(每一个三元列表,表示这些字母的先后顺序),通过分析得到secret字符串

那么这么去做呢?

  1. 首先我们要得到这个字符串的所有字母,根据题意是不会出现secret中没有的字母

    # 遍历triplets,找到secrets
    for i in triplets:
        for j in i:
            # 如果此时的值没有在secrets中,就添加
            if j not in secrets:
                secrets.append(j)
    
  2. 然后我们根据triplets的规则,遍历这个triplets,根据三元列表的顺序,两两进行比较,[0]的位置在[1]的位置的前面,如果[0]在secrets中的位置大于[1],说明此时在secrets中[0]在[1]的后面,所以需要交换位置,当我们执行完了之后,就会得到我们需要的secret,这里有一点需要注意,当我们要处理大量数据的时候,可能会出现没有完全交换的情况,所以我们需要多遍历一下triplets。

    if secrets.index(triplet[i]) > secrets.index(triplet[i + 1]):
                # [0]的位置在[1]的位置的前面,如果[0]在secrets中的位置大于[1],说明此时[0]在[1]的后面,所以需要交换位置
                # 首先删除[0]的位置
                secrets.remove(triplet[i])
                # 把[0]添加到[1]的位置
                secrets.insert(secrets.index(triplet[i + 1]),triplet[i])
    

完整代码

def recoverSecret(triplets):
    print(triplets)
    # 首先不管怎么说,先找到secret字符串,此时不管什么顺序,当我们找到之后,再按照传入的triplets对比交换位置即可
    secrets = []
    # 遍历triplets,找到secrets
    for i in triplets:
        for j in i:
            # 如果此时的值没有在secrets中,就添加
            if j not in secrets:
                secrets.append(j)

    # 在遍历triplets,根据每一个三元组,确定顺序,* 2 是为了防止漏网之鱼
    for triplet in triplets * 2:
        # 两个两个的做比较
        for i in range(0,2):
            # if secrets.index(triplet[0]) > secrets.index(triplet[1]):
            #     # [0]的位置在[1]的位置的前面,如果[0]在secrets中的位置大于[1],说明此时[0]在[1]的后面,所以需要交换位置
            #     # 首先删除[0]的位置
            #     secrets.remove(triplet[0])
            #     # 把[0]添加到[1]的位置
            #     secrets.insert(secrets.index(triplet[1]),triplet[0])
            if secrets.index(triplet[i]) > secrets.index(triplet[i + 1]):
                # [0]的位置在[1]的位置的前面,如果[0]在secrets中的位置大于[1],说明此时[0]在[1]的后面,所以需要交换位置
                # 首先删除[0]的位置
                secrets.remove(triplet[i])
                # 把[0]添加到[1]的位置
                secrets.insert(secrets.index(triplet[i + 1]),triplet[i])
                
    return "".join(secrets)