Skip to content

2024-04-30 v. 5.5.7: added "2351. First Letter to Appear Twice" #614

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
Apr 30, 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 @@ -417,3 +417,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
| 2335. Minimum Amount of Time to Fill Cups | [Link](https://leetcode.com/problems/minimum-amount-of-time-to-fill-cups/) | [Link](./lib/easy/2335_minimum_amount_of_time_to_fill_cups.rb) |
| 2341. Maximum Number of Pairs in Array | [Link](https://leetcode.com/problems/maximum-number-of-pairs-in-array/) | [Link](./lib/easy/2341_maximum_number_of_pairs_in_array.rb) |
| 2347. Best Poker Hand | [Link](https://leetcode.com/problems/best-poker-hand/) | [Link](./lib/easy/2347_best_poker_hand.rb) |
| 2351. First Letter to Appear Twice | [Link](https://leetcode.com/problems/first-letter-to-appear-twice/) | [Link](./lib/easy/2351_first_letter_to_appear_twice.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 = '5.5.6'
s.version = '5.5.7'
s.license = 'MIT'
s.files = ::Dir['lib/**/*.rb'] + %w[bin/leetcode-ruby README.md LICENSE]
s.executable = 'leetcode-ruby'
Expand Down
15 changes: 15 additions & 0 deletions lib/easy/2351_first_letter_to_appear_twice.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

# https://leetcode.com/problems/first-letter-to-appear-twice/
# @param {String} s
# @return {Character}
def repeated_character(s)
letters = ::Array.new(128, 0)
s.each_char do |c|
return c if letters[c.ord] == 1

letters[c.ord] += 1
end

'0'
end
16 changes: 16 additions & 0 deletions test/easy/test_2351_first_letter_to_appear_twice.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

require_relative '../test_helper'
require_relative '../../lib/easy/2351_first_letter_to_appear_twice'
require 'minitest/autorun'

class FirstLetterToAppearTwiceTest < ::Minitest::Test
def test_default
assert_equal('c', repeated_character('abccbaacz'))
assert_equal('d', repeated_character('abcdd'))
end

def test_additional
assert_equal('0', repeated_character('abc'))
end
end
Loading