My 4 lines Python code


  • 3

    The question is straightforward. We only need generate possible substring and count it. When the count larger than 2, we take the substring as result.

    class Solution:
        # @param s, a string
        # @return a list of strings
        def findRepeatedDnaSequences(self, s):
            dictionary = dict()
            for i in [s[x : x + 10] for x in range(len(s) - 9)]:
                dictionary[i] = dictionary.get(i, 0) + 1
            return [k for k, v in dictionary.iteritems() if v > 1]
    

    If we can use Counter, it only needs 1 line code:

    class Solution:
        # @param s, a string
        # @return a list of strings
        def findRepeatedDnaSequences(self, s):
            return [k for k,v in Counter([s[x:x+10] for x in range(len(s)-9)]).iteritems() if v > 1]

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.