12 ms solution in Go


  • 0
    K
    func findRepeatedDnaSequences(s string) []string {
        var codes ['T' + 1]uint32
        codes['A'] = 0
        codes['C'] = 1
        codes['G'] = 2
        codes['T'] = 3
    
        var repeated []string
        seen := make([]byte, 1<<20)
        var cur uint32
        for i := 0; i < len(s); i++ {
            cur = ((cur << 2) & 0xFFFFF) | codes[s[i]]
            if i < 9 {
                continue
            }
            if seen[cur] == 0 {
                seen[cur] = 1
            } else if seen[cur] == 1 {
                repeated = append(repeated, s[i-9:i+1])
                seen[cur] = 2
            }
        }
    
        return repeated
    }

Log in to reply
 

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