Skip to content

Latest commit

 

History

History
33 lines (28 loc) · 979 Bytes

SequenceCountInStringSolution.md

File metadata and controls

33 lines (28 loc) · 979 Bytes

Sequence Count in String Solution

Swift

struct CharSequence {
    var char: Character
    var occurrences: Int
}

func sequenceCount(from str: String) -> String {
    var sequenceArr = [CharSequence]()
    var currentCharSequence: CharSequence?
    for c in str {
        guard currentCharSequence != nil else { currentCharSequence = CharSequence(char: c, occurrences: 1); continue }
        if c == currentCharSequence!.char {
            currentCharSequence!.occurrences += 1
        } else {
            sequenceArr.append(currentCharSequence!)
            currentCharSequence = CharSequence(char: c, occurrences: 1)
        }
    }
    sequenceArr.append(currentCharSequence!)
    return sequenceArr
      .reduce(""){(currentStr, charSequence) in
          return currentStr + ", " + "\(charSequence.char)\(charSequence.occurrences)"
      }
      .trimmingCharacters(in: .punctuationCharacters)
      .trimmingCharacters(in: .whitespacesAndNewlines)
}