softnshare / kata

Code Kata 這個概念是由 The Pragmatic Programmer 的作者之一Dave Thomas提出的, 想要提升自己的coding skill嗎? 歡迎加入這個slack channel, 加入請參考右邊網頁說明
https://softnshare.wordpress.com/slack/kata/
38 stars 4 forks source link

Recover a secret string from random triplets #24

Open bucker opened 8 years ago

bucker commented 8 years ago

Description: 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.

給定一連串的 triplets,推斷出原字串。 一個 triplet 由三個字母組成,順序跟原字串的順序一致。例如:原字串 "whatisup" 的 triplet 可能為 "whi"、"tup"、"hip"... 等。 為了簡化題目,可以假設:

題目網址:https://www.codewars.com/kata/recover-a-secret-string-from-random-triplets

bucker commented 8 years ago

Pass

Harpsichord1207 commented 7 years ago

比如whatisup这个字符串,w是第一个,那么在 triplets里它一定只会出现在第一个位置(index为0),不可能出现在第二个或第三个 因此只要观察所有的 triplets,只在第一个位置出现过的那个字母(而且很容易能理解只有唯一一个)肯定是字符串的第一个 找到第一个后,从所有triplets删除这个字符,有些triplets长度就变成2了,即后面两个往前挪了一位 ,重新寻找只在第一个位置出现的字符,就是原字符串的第二个字符,以此类推,就可以全部按顺序找出来了。 我也是刚刚才完成CodeWar的这道题