Skip to content

Instantly share code, notes, and snippets.

@imouaddine
Created November 10, 2014 14:32
Show Gist options
  • Save imouaddine/70ac11f6e9f6a55cc3b0 to your computer and use it in GitHub Desktop.
Save imouaddine/70ac11f6e9f6a55cc3b0 to your computer and use it in GitHub Desktop.
def compress_length(a)
if a.length < 3
return a
end
result = ""
count = 1
a.chars.each_with_index do |c, index|
if c == a[index+1]
count += 1
else
result << "#{c}#{count}"
count = 1
end
end
result.length >= a.length ? a : result
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment