diff --git a/README.md b/README.md index 99615b39..abfe6a19 100644 --- a/README.md +++ b/README.md @@ -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) | diff --git a/leetcode-ruby.gemspec b/leetcode-ruby.gemspec index 6e77e22b..6e78915f 100644 --- a/leetcode-ruby.gemspec +++ b/leetcode-ruby.gemspec @@ -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' diff --git a/lib/medium/443_string_compression.rb b/lib/medium/443_string_compression.rb new file mode 100644 index 00000000..725a4b66 --- /dev/null +++ b/lib/medium/443_string_compression.rb @@ -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 diff --git a/test/medium/test_443_string_compression.rb b/test/medium/test_443_string_compression.rb new file mode 100644 index 00000000..be5b6d0e --- /dev/null +++ b/test/medium/test_443_string_compression.rb @@ -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