Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -573,3 +573,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
| 437. Path Sum III | [Link](https://leetcode.com/problems/path-sum-iii/) | [Link](./lib/medium/437_path_sum_iii.rb) | [Link](./test/medium/test_437_path_sum_iii.rb) |
| 438. Find All Anagrams in a String | [Link](https://leetcode.com/problems/find-all-anagrams-in-a-string/) | [Link](./lib/medium/438_find_all_anagrams_in_a_string.rb) | [Link](./test/medium/test_438_find_all_anagrams_in_a_string.rb) |
| 442. Find All Duplicates in an Array | [Link](https://leetcode.com/problems/find-all-duplicates-in-an-array/) | [Link](./lib/medium/442_find_all_duplicates_in_an_array.rb) | [Link](./test/medium/test_442_find_all_duplicates_in_an_array.rb) |
| 443. String Compression | [Link](https://leetcode.com/problems/string-compression/) | [Link](./lib/medium/443_string_compression.rb) | [Link](./test/medium/test_443_string_compression.rb) |
2 changes: 1 addition & 1 deletion leetcode-ruby.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ require 'English'
::Gem::Specification.new do |s|
s.required_ruby_version = '>= 3.0'
s.name = 'leetcode-ruby'
s.version = '7.0.7'
s.version = '7.0.8'
s.license = 'MIT'
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
s.executable = 'leetcode-ruby'
Expand Down
28 changes: 28 additions & 0 deletions lib/medium/443_string_compression.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# frozen_string_literal: true

# https://leetcode.com/problems/string-compression/
# @param {Character[]} chars
# @return {Integer}
def compress(chars)
prev = chars[0]
prev_count = 0
chars_with_count = []
chars.each do |char|
if char == prev
prev_count += 1
else
chars_with_count << prev
prev_count.to_s.each_char { |c| chars_with_count << c } if prev_count > 1

prev = char
prev_count = 1
end
end

chars_with_count << prev
prev_count.to_s.each_char { |c| chars_with_count << c } if prev_count > 1

(0...chars_with_count.size).each { |i| chars[i] = chars_with_count[i] }

chars_with_count.size
end
28 changes: 28 additions & 0 deletions test/medium/test_443_string_compression.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# frozen_string_literal: true

require_relative '../test_helper'
require_relative '../../lib/medium/443_string_compression'
require 'minitest/autorun'

class StringCompressionTest < ::Minitest::Test
def test_default_one
input = %w[a a b b c c c]

assert_equal(6, compress(input))
assert_equal(%w[a 2 b 2 c 3 c], input)
end

def test_default_two
input = ['a']

assert_equal(1, compress(input))
assert_equal(['a'], input)
end

def test_default_three
input = %w[a b b b b b b b b b b b b]

assert_equal(4, compress(input))
assert_equal(%w[a b 1 2 b b b b b b b b b], input)
end
end
Loading