Skip to content

Commit 7a22f26

Browse files
committed
2024-07-10 v. 6.1.1: added "38. Count and Say"
1 parent 0c7035b commit 7a22f26

File tree

4 files changed

+44
-1
lines changed

4 files changed

+44
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,3 +476,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
476476
| 33. Search in Rotated Sorted Array | [Link](https://leetcode.com/problems/search-in-rotated-sorted-array/) | [Link](./lib/medium/33_search_in_rotated_sorted_array.rb) |
477477
| 34. Find First and Last Position of Element in Sorted Array | [Link](https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/) | [Link](./lib/medium/34_find_first_and_last_position_of_element_in_sorted_array.rb) |
478478
| 36. Valid Sudoku | [Link](https://leetcode.com/problems/valid-sudoku/) | [Link](./lib/medium/36_valid_sudoku.rb) |
479+
| 38. Count and Say | [Link](https://leetcode.com/problems/count-and-say/) | [Link](./lib/medium/38_count_and_say.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 = '6.1.0'
8+
s.version = '6.1.1'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[bin/leetcode-ruby README.md LICENSE]
1111
s.executable = 'leetcode-ruby'

lib/medium/38_count_and_say.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# frozen_string_literal: true
2+
3+
# https://leetcode.com/problems/count-and-say/
4+
# @param {Integer} n
5+
# @return {String}
6+
def count_and_say(n)
7+
return '1' if n == 1
8+
9+
str = count_and_say(n - 1)
10+
result = []
11+
count = 1
12+
i = 1
13+
while i < str.length
14+
if str[i] == str[i - 1]
15+
count += 1
16+
else
17+
result << (count + 48).chr
18+
result << str[i - 1]
19+
20+
count = 1
21+
end
22+
23+
i += 1
24+
end
25+
26+
result << (count + 48).chr
27+
result << str[i - 1]
28+
29+
result.join
30+
end

test/medium/test_38_count_and_say.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../test_helper'
4+
require_relative '../../lib/medium/38_count_and_say'
5+
require 'minitest/autorun'
6+
7+
class CountAndSayTest < ::Minitest::Test
8+
def test_default
9+
assert_equal('1211', count_and_say(4))
10+
assert_equal('1', count_and_say(1))
11+
end
12+
end

0 commit comments

Comments
 (0)