Created
March 9, 2022 15:55
-
-
Save arturdent/d85aed5ff2fb9ee540bff694337df9f0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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