-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSolution.swift
More file actions
43 lines (38 loc) · 1.23 KB
/
Solution.swift
File metadata and controls
43 lines (38 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/*
String Compression
Given an array of characters, compress it in-place.
The length after compression must always be smaller than or equal to the original array.
Every element of the array should be a character (not int) of length 1.
After you are done modifying the input array in-place, return the new length of the array.
*/
func compress(_ chars: inout [Character]) -> Int {
var left = 0
var right = 0
var write = 0
for i in 0...chars.count {
if i == chars.count {
let sum = right - left
writeLetter(chars[left], sum, &write, &chars)
} else {
if chars[left] != chars[right] {
let sum = right - left
writeLetter(chars[left], sum, &write, &chars)
left = right
}
}
right += 1
}
return write
}
func writeLetter(_ c: Character, _ sum: Int, _ index: inout Int, _ arr: inout [Character]) {
arr[index] = c
index += 1
if sum > 9 {
arr[index] = [Character](String(sum / 10))[0]
arr[index + 1] = [Character](String(sum % 10))[0]
index += 2
} else if sum > 1 {
arr[index] = [Character](String(sum))[0]
index += 1
}
}