Swift 3 Dictionary solution


  • 0
    Y
    class Solution {
        func findRepeatedDnaSequences(_ s: String) -> [String] {
            // find the 10-seq and save to hashmap<String, Int(count)> save the unique ones into list
            if s.characters.count <= 10 {
                return []
            }
            let chars = Array(s.characters)
            var dic = [String: Int]()
            var ans = [String]()
            for i in 0...(chars.count - 10) {
                let seq = String(chars[i..<i+10])
                if let v = dic[seq] {
                    if v == 1 {
                        ans.append(seq)
                    }
                    dic[seq] = v + 1
                } else {
                    dic[seq] = 1
                }
            }
            return ans
        }
    }
    
    

Log in to reply
 

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