250

解答

class DNASequence:
    def longestDNASequence(self, sequence):
        dna = {"A", "C", "G", "T"}
        current = 0
        res = 0
        for s in sequence:
            if s in dna:
                current += 1
                res = max(current, res)
            else:
                res = max(current, res)
                current = 0

        return res

550

方針

  • itertools.productを使って組み合わせをすべて洗い出す
  • 洗い出した組み合わせから、文字列の部分列を取り除いていく
  • 組み合わせが一つでも残っていたらそれが答え

解答

久しぶりに解答できた。

class TopBiologist:
    def findShortestNewSequence(self, sequence):
        letter = ['A', 'C', 'G', 'T']

        flag = True
        length = 0

        while(flag):
            length += 1
            s = set()
            res = ""
            for i in itertools.product(letter, repeat=length):
                s.add(''.join(i))

            for i in range(len(sequence) - length + 1):
                if sequence[i:i + length] in s:
                    s.remove(sequence[i:i + length])

            if len(s) > 0:
                res = s.pop()
                flag = False

        return res