Skip to content

Instantly share code, notes, and snippets.

@arturdent
Created March 9, 2022 15:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arturdent/d85aed5ff2fb9ee540bff694337df9f0 to your computer and use it in GitHub Desktop.
Save arturdent/d85aed5ff2fb9ee540bff694337df9f0 to your computer and use it in GitHub Desktop.
func lengthEncoding(str: String){
let chrArr = Array(str)
var count = 0
var previous : Character = chrArr[0]
var finalString = ""
for (_, item) in chrArr.enumerated() {
if previous == item{
count += 1
}else{
finalString = finalString + previous.description + count.description
count = 1
}
previous = item
}
print(finalString + previous.description + count.description)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment