-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompressed.rb
32 lines (30 loc) · 877 Bytes
/
compressed.rb
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
# implement a method to preform basic string compression using the counts of repeated characters.
# Example:
# input: 'aabcccccaaa' returns: 'a2b1c5a3'
# If the compressed string would not become smaller than the original string then the method should return the original string.
# You can assume that the string given will only contain uppercase and lowercase letters (a-z)
def compressed(string)
strArray = string.split('')
compressed = []
i = 0
count = 1
while i < strArray.length
if strArray[i] != strArray[(i + 1)]
compressed << strArray[i]
compressed << count
count = 1
else
count += 1
end
i += 1
end
compressed = compressed.join()
if compressed.length < string.length
p compressed
else
p string
end
end
compressed('abcdefggg')
compressed('abcdefgggaabb')
compressed('aabcccccaaa')