diff --git a/README.md b/README.md index 5c8b83cb..23efb298 100644 --- a/README.md +++ b/README.md @@ -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) | diff --git a/leetcode-ruby.gemspec b/leetcode-ruby.gemspec index 93169db0..ff8d7c70 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 = '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' diff --git a/lib/easy/2351_first_letter_to_appear_twice.rb b/lib/easy/2351_first_letter_to_appear_twice.rb new file mode 100644 index 00000000..48f53a23 --- /dev/null +++ b/lib/easy/2351_first_letter_to_appear_twice.rb @@ -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 diff --git a/test/easy/test_2351_first_letter_to_appear_twice.rb b/test/easy/test_2351_first_letter_to_appear_twice.rb new file mode 100644 index 00000000..08ff32ce --- /dev/null +++ b/test/easy/test_2351_first_letter_to_appear_twice.rb @@ -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