swiftsubstringswift6string.index

How to make Substring keys with String.Index for count all occurrences of all size 2 substrings


I'm not familiar with String.Index, is there a better way to make the substring keys than this:

let a = "GGCCGGGCGCGGTGGCTCACGCCTGTAATCCCAGCACTTTGGGAGGCCGAGGCGGGCGGA"

let keysize = 2
let size = a.count + 1 - keysize
var counts: [String: Int] = [:]

var i = 0
while i < size {
   let start_offset = a.index(seq.startIndex, offsetBy: i)
   let end = a.index(start_offset, offsetBy: keysize)     
   if let key = String( a[start_offset..<end] ) {   
      if let v = counts[key] {   
         counts[key] = v + 1  
      } else {
         counts[key] = 1 
      }
   }
   i += 1     
}

for (k,v) in counts {
   print("\(k): \(v)")   
}

Result:

CC: 5
TA: 1
TG: 3
GC: 9
CG: 7
GT: 2
GA: 3
CA: 3
AC: 2
TC: 2
AG: 3
AT: 1
TT: 2
GG: 12
AA: 1
CT: 3

Solution

  • You can try using this. Tested in Xcode 16.0

    let seq = "GGCCGGGCGCGGTGGCTCACGCCTGTAATCCCAGCACTTTGGGAGGCCGAGGCGGGCGGA"
    let keysize = 2
    var counts: [String: Int] = [:] 
    for i in seq.indices.dropLast(keysize - 1) { 
    let key = String(seq[i..<seq.index(i, offsetBy: keysize)])
     counts[key, default: 0] += 1 
    }