-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayground_compress
More file actions
51 lines (31 loc) · 1.18 KB
/
playground_compress
File metadata and controls
51 lines (31 loc) · 1.18 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
43
44
45
46
47
48
let input = "AAABBACCiurioweurowiquroqwiuroiweurljklsdkldskllksdklasdklkkkddkddddddsd--000"
var numberLetter = Int(input.count)
var compress:String = ""
var lastLetter:String = ""
var totalLetter:Int = 0
func concatString(_ compress:String , _ wordConcat:String, _ countString:Int)->String {
if(totalLetter == 1) {
return compress + wordConcat
}
else {
return compress + lastLetter + String(countString)
}
}
for i in 0..<numberLetter {
let index = input.index(input.startIndex,offsetBy:i)
let currentword:String = String(input[index])
//Fist time in loop it`s necessary set lastword equals current word
if(i == 0){
lastLetter = currentword
}
//It`s change current word and cont > 0, registre new item for concat
if (currentword != lastLetter && totalLetter > 0 ) {
compress = concatString(compress,lastLetter,totalLetter)
lastLetter = currentword
totalLetter = 0
}
totalLetter = totalLetter + 1
}
//After exit to loop we have registre te last letter computing
compress = concatString(compress,lastLetter,totalLetter)
print(compress)