From 6cfe4b1dbadf9ee34c32505228a59a66d9d37b25 Mon Sep 17 00:00:00 2001 From: fartem Date: Mon, 10 Mar 2025 09:25:23 +0300 Subject: [PATCH] 2025-03-10 v. 8.8.9: added "1910. Remove All Occurrences of a Substring" --- README.md | 1 + leetcode-ruby.gemspec | 2 +- ...0_remove_all_occurrences_of_a_substring.rb | 17 ++++++++++++ ...0_remove_all_occurrences_of_a_substring.rb | 27 +++++++++++++++++++ 4 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 lib/medium/1910_remove_all_occurrences_of_a_substring.rb create mode 100644 test/medium/test_1910_remove_all_occurrences_of_a_substring.rb diff --git a/README.md b/README.md index cab6abfa..2cfa8f3b 100644 --- a/README.md +++ b/README.md @@ -722,6 +722,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/). | 1834. Single-Threaded CPU | [Link](https://leetcode.com/problems/single-threaded-cpu/) | [Link](./lib/medium/1834_single_threaded_cpu.rb) | [Link](./test/medium/test_1834_single_threaded_cpu.rb) | | 1845. Seat Reservation Manager | [Link](https://leetcode.com/problems/seat-reservation-manager/) | [Link](./lib/medium/1845_seat_reservation_manager.rb) | [Link](./test/medium/test_1845_seat_reservation_manager.rb) | | 1877. Minimize Maximum Pair Sum in Array | [Link](https://leetcode.com/problems/minimize-maximum-pair-sum-in-array/) | [Link](./lib/medium/1877_minimize_maximum_pair_sum_in_array.rb) | [Link](./test/medium/test_1877_minimize_maximum_pair_sum_in_array.rb) | +| 1910. Remove All Occurrences of a Substring | [Link](https://leetcode.com/problems/remove-all-occurrences-of-a-substring/) | [Link](./lib/medium/1910_remove_all_occurrences_of_a_substring.rb) | [Link](./test/medium/test_1910_remove_all_occurrences_of_a_substring.rb) | | 2116. Check if a Parentheses String Can Be Valid | [Link](https://leetcode.com/problems/check-if-a-parentheses-string-can-be-valid/) | [Link](./lib/medium/2116_check_if_a_parentheses_string_can_be_valid.rb) | [Link](./test/medium/test_2116_check_if_a_parentheses_string_can_be_valid.rb) | | 2425. Bitwise XOR of All Pairings | [Link](https://leetcode.com/problems/bitwise-xor-of-all-pairings/) | [Link](./lib/medium/2425_bitwise_xor_of_all_pairings.rb) | [Link](./test/medium/test_2425_bitwise_xor_of_all_pairings.rb) | | 2429. Minimize XOR | [Link](https://leetcode.com/problems/minimize-xor/) | [Link](./lib/medium/2429_minimize_xor.rb) | [Link](./test/medium/test_2429_minimize_xor.rb) | diff --git a/leetcode-ruby.gemspec b/leetcode-ruby.gemspec index 20beb16b..5fc8cd6b 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 = '8.8.8' + s.version = '8.8.9' s.license = 'MIT' s.files = ::Dir['lib/**/*.rb'] + %w[README.md] s.executable = 'leetcode-ruby' diff --git a/lib/medium/1910_remove_all_occurrences_of_a_substring.rb b/lib/medium/1910_remove_all_occurrences_of_a_substring.rb new file mode 100644 index 00000000..e39d8c95 --- /dev/null +++ b/lib/medium/1910_remove_all_occurrences_of_a_substring.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +# https://leetcode.com/problems/remove-all-occurrences-of-a-substring/ +# @param {String} s +# @param {String} part +# @return {String} +def remove_occurrences(s, part) + result = s.dup + index = result.index(part) + + while index + result.slice!(index, part.length) + index = result.index(part) + end + + result +end diff --git a/test/medium/test_1910_remove_all_occurrences_of_a_substring.rb b/test/medium/test_1910_remove_all_occurrences_of_a_substring.rb new file mode 100644 index 00000000..5b82fc1e --- /dev/null +++ b/test/medium/test_1910_remove_all_occurrences_of_a_substring.rb @@ -0,0 +1,27 @@ +# frozen_string_literal: true + +require_relative '../test_helper' +require_relative '../../lib/medium/1910_remove_all_occurrences_of_a_substring' +require 'minitest/autorun' + +class RemoveAllOccurrencesOfASubstringTest < ::Minitest::Test + def test_default_one + assert_equal( + 'dab', + remove_occurrences( + 'daabcbaabcbc', + 'abc' + ) + ) + end + + def test_default_two + assert_equal( + 'ab', + remove_occurrences( + 'axxxxyyyyb', + 'xy' + ) + ) + end +end