My concise python solution


  • 0
    class Solution:
        # @param s, a string
        # @return a list of strings
        def findRepeatedDnaSequences(self, s):
            dic ,ans, n=set(),set(),len(s)
            for i in xrange(n-9):
                cur = s[i:i+10]
                if cur in dic:
                    ans.add(cur)
                else: dic.add(cur)
            return list(ans)

  • 0
    T

    But what about

    s = "a a a a a a a a a a a c g c g c g c g c g c g c g c g c g c g c g"
    

    i.e. 11x "a"?

    At the end of first iteration:

    i = 0
    cur = "a a a a a a a a a a"
    dic = {"a a a a a a a a a a"}
    ans = {}
    

    At the end of second iteration:

    i = 1
    cur = "a a a a a a a a a a"
    dic = {"a a a a a a a a a a"}
    ans = {"a a a a a a a a a a"}
    

    Which is wrong since "a a a a a a a a a a" does not repeat.


  • 0

    you problem maybe whether we can let the sequence which having the same index to be answer . In my answer, I let 0 to 9 be the first one,and 1 to 10 be second.If you don't think so , when you meet the sequence which not in dic,you can let the next index to be current index+10.


  • 0
    T

    Actually, in the case I proposed we have 11x the letter "a". At i=0, your cur="a"*10 and at i=1, cur="a"*10 once again. That only happens in this specific case where we have 10+ repeated letters.


Log in to reply
 

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