Chapter1 1.5
計算量O(n)のつもり
compress.rb
class String
def compress
temp_v = ""
temp_arr = []
count = 1
for s in 0..self.length
if s == 0
temp_v = self[s]
next
else
if temp_v == self[s]
count += 1
else
temp_arr << temp_v + count.to_s
temp_v = self[s]
count = 1
end
end
end
if temp_arr.join.length == self.length
self
else
temp_arr.join
end
end
end
str = "abbabcccccaaa"
puts str.compress