Skip to content

Commit 5cc6769

Browse files
authored
Merge pull request #778 from fartem/443_String_Compression
2024-11-21 v. 7.0.8: added "443. String Compression"
2 parents 76a8bfb + a211a4a commit 5cc6769

File tree

4 files changed

+58
-1
lines changed

4 files changed

+58
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,3 +573,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
573573
| 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) |
574574
| 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) |
575575
| 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) |
576+
| 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) |

leetcode-ruby.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ require 'English'
55
::Gem::Specification.new do |s|
66
s.required_ruby_version = '>= 3.0'
77
s.name = 'leetcode-ruby'
8-
s.version = '7.0.7'
8+
s.version = '7.0.8'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
1111
s.executable = 'leetcode-ruby'

lib/medium/443_string_compression.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# frozen_string_literal: true
2+
3+
# https://leetcode.com/problems/string-compression/
4+
# @param {Character[]} chars
5+
# @return {Integer}
6+
def compress(chars)
7+
prev = chars[0]
8+
prev_count = 0
9+
chars_with_count = []
10+
chars.each do |char|
11+
if char == prev
12+
prev_count += 1
13+
else
14+
chars_with_count << prev
15+
prev_count.to_s.each_char { |c| chars_with_count << c } if prev_count > 1
16+
17+
prev = char
18+
prev_count = 1
19+
end
20+
end
21+
22+
chars_with_count << prev
23+
prev_count.to_s.each_char { |c| chars_with_count << c } if prev_count > 1
24+
25+
(0...chars_with_count.size).each { |i| chars[i] = chars_with_count[i] }
26+
27+
chars_with_count.size
28+
end
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../test_helper'
4+
require_relative '../../lib/medium/443_string_compression'
5+
require 'minitest/autorun'
6+
7+
class StringCompressionTest < ::Minitest::Test
8+
def test_default_one
9+
input = %w[a a b b c c c]
10+
11+
assert_equal(6, compress(input))
12+
assert_equal(%w[a 2 b 2 c 3 c], input)
13+
end
14+
15+
def test_default_two
16+
input = ['a']
17+
18+
assert_equal(1, compress(input))
19+
assert_equal(['a'], input)
20+
end
21+
22+
def test_default_three
23+
input = %w[a b b b b b b b b b b b b]
24+
25+
assert_equal(4, compress(input))
26+
assert_equal(%w[a b 1 2 b b b b b b b b b], input)
27+
end
28+
end

0 commit comments

Comments
 (0)