diff --git a/README.md b/README.md index 724ad026..187424ea 100644 --- a/README.md +++ b/README.md @@ -668,6 +668,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/). | 1161. Maximum Level Sum of a Binary Tree | [Link](https://leetcode.com/problems/maximum-level-sum-of-a-binary-tree/) | [Link](./lib/medium/1161_maximum_level_sum_of_a_binary_tree.rb) | [Link](./test/medium/test_1161_maximum_level_sum_of_a_binary_tree.rb) | | 1171. Remove Zero Sum Consecutive Nodes from Linked List | [Link](https://leetcode.com/problems/remove-zero-sum-consecutive-nodes-from-linked-list/) | [Link](./lib/medium/1171_remove_zero_sum_consecutive_nodes_from_linked_list.rb) | [Link](./test/medium/test_1171_remove_zero_sum_consecutive_nodes_from_linked_list.rb) | | 1209. Remove All Adjacent Duplicates in String II | [Link](https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string-ii/) | [Link](./lib/medium/1209_remove_all_adjacent_duplicates_in_string_ii.rb) | [Link](./test/medium/test_1209_remove_all_adjacent_duplicates_in_string_ii.rb) | +| 1247. Minimum Swaps to Make Strings Equal | [Link](https://leetcode.com/problems/minimum-swaps-to-make-strings-equal/) | [Link](./lib/medium/1247_minimum_swaps_to_make_strings_equal.rb) | [Link](./test/medium/test_1247_minimum_swaps_to_make_strings_equal.rb) | | 1400. Construct K Palindrome Strings | [Link](https://leetcode.com/problems/construct-k-palindrome-strings/) | [Link](./lib/medium/1400_construct_k_palindrome_strings.rb) | [Link](./test/medium/test_1400_construct_k_palindrome_strings.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) | diff --git a/leetcode-ruby.gemspec b/leetcode-ruby.gemspec index a2b253df..f0bc4b52 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.3.5' + s.version = '8.3.6' s.license = 'MIT' s.files = ::Dir['lib/**/*.rb'] + %w[README.md] s.executable = 'leetcode-ruby' diff --git a/lib/medium/1247_minimum_swaps_to_make_strings_equal.rb b/lib/medium/1247_minimum_swaps_to_make_strings_equal.rb new file mode 100644 index 00000000..cf09d894 --- /dev/null +++ b/lib/medium/1247_minimum_swaps_to_make_strings_equal.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +# https://leetcode.com/problems/minimum-swaps-to-make-strings-equal/ +# @param {String} s1 +# @param {String} s2 +# @return {Integer} +def minimum_swap(s1, s2) + x = 0 + y = 0 + + s1.each_char.with_index do |char, i| + next unless char != s2[i] + + if char == 'x' + x += 1 + else + y += 1 + end + end + + x_remainder = x % 2 + y_remainder = y % 2 + + if x_remainder.zero? && y_remainder.zero? + (x + y) / 2 + elsif x_remainder == 1 && y_remainder == 1 + (x + y) / 2 + 1 + else + -1 + end +end diff --git a/test/medium/test_1247_minimum_swaps_to_make_strings_equal.rb b/test/medium/test_1247_minimum_swaps_to_make_strings_equal.rb new file mode 100644 index 00000000..a21fc2c8 --- /dev/null +++ b/test/medium/test_1247_minimum_swaps_to_make_strings_equal.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +require_relative '../test_helper' +require_relative '../../lib/medium/1247_minimum_swaps_to_make_strings_equal' +require 'minitest/autorun' + +class MinimumSwapsToMakeStringsEqualTest < ::Minitest::Test + def test_default_one = assert_equal(1, minimum_swap('xx', 'yy')) + + def test_default_two = assert_equal(2, minimum_swap('xy', 'yx')) + + def test_default_three = assert_equal(-1, minimum_swap('xx', 'xy')) +end