From ce3ca0a62088a79f5f6c544608e8743b025ada61 Mon Sep 17 00:00:00 2001 From: fartem Date: Thu, 17 Apr 2025 12:44:32 +0300 Subject: [PATCH] 2025-04-17 v. 9.2.8: added "3083. Existence of a Substring in a String and Its Reverse" --- README.md | 1 + leetcode-ruby.gemspec | 2 +- ...a_substring_in_a_string_and_its_reverse.rb | 17 ++++++++++ ...a_substring_in_a_string_and_its_reverse.rb | 31 +++++++++++++++++++ 4 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 lib/easy/3083_existence_of_a_substring_in_a_string_and_its_reverse.rb create mode 100644 test/easy/test_3083_existence_of_a_substring_in_a_string_and_its_reverse.rb diff --git a/README.md b/README.md index 9239f3ac..2812e622 100644 --- a/README.md +++ b/README.md @@ -455,6 +455,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/). | 2549. Count Distinct Numbers on Board | [Link](https://leetcode.com/problems/count-distinct-numbers-on-board/) | [Link](./lib/easy/2549_count_distinct_numbers_on_board.rb) | [Link](./test/easy/test_2549_count_distinct_numbers_on_board.rb) | | 2652. Sum Multiples | [Link](https://leetcode.com/problems/sum-multiples/) | [Link](./lib/easy/2652_sum_multiples.rb) | [Link](./test/easy/test_2652_sum_multiples.rb) | | 2974. Minimum Number Game | [Link](https://leetcode.com/problems/minimum-number-game/) | [Link](./lib/easy/2974_minimum_number_game.rb) | [Link](./test/easy/test_2974_minimum_number_game.rb) | +| 3083. Existence of a Substring in a String and Its Reverse | [Link](https://leetcode.com/problems/existence-of-a-substring-in-a-string-and-its-reverse/) | [Link](./lib/easy/3083_existence_of_a_substring_in_a_string_and_its_reverse.rb) | [Link](./test/easy/test_3083_existence_of_a_substring_in_a_string_and_its_reverse.rb) | | 3110. Score of a String | [Link](https://leetcode.com/problems/score-of-a-string/) | [Link](./lib/easy/3110_score_of_a_string.rb) | [Link](./test/easy/test_3110_score_of_a_string.rb) | | 3136. Valid Word | [Link](https://leetcode.com/problems/valid-word/) | [Link](./lib/easy/3136_valid_word.rb) | [Link](./test/easy/test_3136_valid_word.rb) | | 3151. Special Array I | [Link](https://leetcode.com/problems/special-array-i/) | [Link](./lib/easy/3151_special_array_i.rb) | [Link](./test/easy/test_3151_special_array_i.rb) | diff --git a/leetcode-ruby.gemspec b/leetcode-ruby.gemspec index 962ff221..807ec091 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 = '9.2.7' + s.version = '9.2.8' s.license = 'MIT' s.files = ::Dir['lib/**/*.rb'] + %w[README.md] s.executable = 'leetcode-ruby' diff --git a/lib/easy/3083_existence_of_a_substring_in_a_string_and_its_reverse.rb b/lib/easy/3083_existence_of_a_substring_in_a_string_and_its_reverse.rb new file mode 100644 index 00000000..716b3ede --- /dev/null +++ b/lib/easy/3083_existence_of_a_substring_in_a_string_and_its_reverse.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +# https://leetcode.com/problems/existence-of-a-substring-in-a-string-and-its-reverse/ +# @param {String} s +# @return {Boolean} +def is_substring_present(s) + return false if s.size < 2 + + rev = s.reverse + subs = [] + + (0..s.length - 2).each do |i| + subs << s[i, 2] + end + + subs.any? { |sub| rev.include?(sub) } +end diff --git a/test/easy/test_3083_existence_of_a_substring_in_a_string_and_its_reverse.rb b/test/easy/test_3083_existence_of_a_substring_in_a_string_and_its_reverse.rb new file mode 100644 index 00000000..8b853003 --- /dev/null +++ b/test/easy/test_3083_existence_of_a_substring_in_a_string_and_its_reverse.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +require_relative '../test_helper' +require_relative '../../lib/easy/3083_existence_of_a_substring_in_a_string_and_its_reverse' +require 'minitest/autorun' + +class ExistenceOfASubstringInAStringAndItsReverseTest < ::Minitest::Test + def test_default_one + assert( + is_substring_present( + 'leetcode' + ) + ) + end + + def test_default_two + assert( + is_substring_present( + 'abcba' + ) + ) + end + + def test_default_three + assert( + !is_substring_present( + 'abcd' + ) + ) + end +end