Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // MARK: - 443. String Compression
- // Сжать строку
- func compress(_ chars: inout [Character]) -> Int {
- // i - чтобы итерироваться по масиву
- var i = 0
- var r = 0
- while i < chars.count {
- var currentChar = chars[i]
- var currentOccur = 0
- while i < chars.count && chars[i] == currentChar {
- currentOccur += 1
- i += 1
- }
- chars[r] = currentChar
- r += 1
- if currentOccur > 1 {
- let currOccurStr = String(currentOccur)
- for num in currOccurStr {
- chars[r] = num
- r += 1
- }
- }
- }
- return r
- }
- var a: [Character] = ["a","a","b","b","c","c","c","c"]
- compress(&a)
- print(a)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement