Skip to content

2024-07-10 v. 6.1.1: added "38. Count and Say" #668

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 10, 2024
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 @@ -476,3 +476,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
| 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) |
| 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) |
| 36. Valid Sudoku | [Link](https://leetcode.com/problems/valid-sudoku/) | [Link](./lib/medium/36_valid_sudoku.rb) |
| 38. Count and Say | [Link](https://leetcode.com/problems/count-and-say/) | [Link](./lib/medium/38_count_and_say.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 = '6.1.0'
s.version = '6.1.1'
s.license = 'MIT'
s.files = ::Dir['lib/**/*.rb'] + %w[bin/leetcode-ruby README.md LICENSE]
s.executable = 'leetcode-ruby'
Expand Down
30 changes: 30 additions & 0 deletions lib/medium/38_count_and_say.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# frozen_string_literal: true

# https://leetcode.com/problems/count-and-say/
# @param {Integer} n
# @return {String}
def count_and_say(n)
return '1' if n == 1

str = count_and_say(n - 1)
result = []
count = 1
i = 1
while i < str.length
if str[i] == str[i - 1]
count += 1
else
result << (count + 48).chr
result << str[i - 1]

count = 1
end

i += 1
end

result << (count + 48).chr
result << str[i - 1]

result.join
end
12 changes: 12 additions & 0 deletions test/medium/test_38_count_and_say.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

require_relative '../test_helper'
require_relative '../../lib/medium/38_count_and_say'
require 'minitest/autorun'

class CountAndSayTest < ::Minitest::Test
def test_default
assert_equal('1211', count_and_say(4))
assert_equal('1', count_and_say(1))
end
end
Loading