diff --git a/README.md b/README.md index 51407117..85312e48 100644 --- a/README.md +++ b/README.md @@ -733,6 +733,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/). | 2091. Removing Minimum and Maximum From Array | [Link](https://leetcode.com/problems/removing-minimum-and-maximum-from-array/) | [Link](./lib/medium/2091_removing_minimum_and_maximum_from_array.rb) | [Link](./test/medium/test_2091_removing_minimum_and_maximum_from_array.rb) | | 2095. Delete the Middle Node of a Linked List | [Link](https://leetcode.com/problems/delete-the-middle-node-of-a-linked-list/) | [Link](./lib/medium/2095_delete_the_middle_node_of_a_linked_list.rb) | [Link](./test/medium/test_2095_delete_the_middle_node_of_a_linked_list.rb) | | 2096. Step-By-Step Directions From a Binary Tree Node to Another | [Link](https://leetcode.com/problems/step-by-step-directions-from-a-binary-tree-node-to-another/) | [Link](./lib/medium/2096_step_by_step_directions_from_a_binary_tree_node_to_another.rb) | [Link](./test/medium/test_2096_step_by_step_directions_from_a_binary_tree_node_to_another.rb) | +| 2109. Adding Spaces to a String | [Link](https://leetcode.com/problems/adding-spaces-to-a-string/) | [Link](./lib/medium/2109_adding_spaces_to_a_string.rb) | [Link](./test/medium/test_2109_adding_spaces_to_a_string.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 06b681bf..165932fc 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.9.9' + s.version = '9.0.0' s.license = 'MIT' s.files = ::Dir['lib/**/*.rb'] + %w[README.md] s.executable = 'leetcode-ruby' diff --git a/lib/medium/2109_adding_spaces_to_a_string.rb b/lib/medium/2109_adding_spaces_to_a_string.rb new file mode 100644 index 00000000..b27c7e62 --- /dev/null +++ b/lib/medium/2109_adding_spaces_to_a_string.rb @@ -0,0 +1,20 @@ +# frozen_string_literal: true + +# https://leetcode.com/problems/adding-spaces-to-a-string/ +# @param {String} s +# @param {Integer[]} spaces +# @return {String} +def add_spaces(s, spaces) + start = 0 + result = [] + + spaces.each do |space| + result << s[start...space] + result << ' ' + start = space + end + + result << s[start..] + + result.join +end diff --git a/test/medium/test_2109_adding_spaces_to_a_string.rb b/test/medium/test_2109_adding_spaces_to_a_string.rb new file mode 100644 index 00000000..2a240416 --- /dev/null +++ b/test/medium/test_2109_adding_spaces_to_a_string.rb @@ -0,0 +1,37 @@ +# frozen_string_literal: true + +require_relative '../test_helper' +require_relative '../../lib/medium/2109_adding_spaces_to_a_string' +require 'minitest/autorun' + +class AddingSpacesToAStringTest < ::Minitest::Test + def test_default_one + assert_equal( + 'Leetcode Helps Me Learn', + add_spaces( + 'LeetcodeHelpsMeLearn', + [8, 13, 15] + ) + ) + end + + def test_default_two + assert_equal( + 'i code in py thon', + add_spaces( + 'icodeinpython', + [1, 5, 7, 9] + ) + ) + end + + def test_default_three + assert_equal( + ' s p a c i n g', + add_spaces( + 'spacing', + [0, 1, 2, 3, 4, 5, 6] + ) + ) + end +end