Skip to content

Commit d67655b

Browse files
authored
2025-02-11 v. 8.5.0: added "1347. Minimum Number of Steps to Make Two Strings Anagram"
2 parents 2994f28 + 997ccae commit d67655b

File tree

4 files changed

+63
-1
lines changed

4 files changed

+63
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
682682
| 1338. Reduce Array Size to The Half | [Link](https://leetcode.com/problems/reduce-array-size-to-the-half/) | [Link](./lib/medium/1338_reduce_array_size_to_the_half.rb) | [Link](./test/medium/test_1338_reduce_array_size_to_the_half.rb) |
683683
| 1339. Maximum Product of Splitted Binary Tree | [Link](https://leetcode.com/problems/maximum-product-of-splitted-binary-tree/) | [Link](./lib/medium/1339_maximum_product_of_splitted_binary_tree.rb) | [Link](./test/medium/test_1339_maximum_product_of_splitted_binary_tree.rb) |
684684
| 1343. Number of Sub-arrays of Size K and Average Greater than or Equal to Threshold | [Link](https://leetcode.com/problems/number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold/) | [Link](./lib/medium/1343_number_of_sub_arrays_of_size_k_and_average_greater_than_or_equal_to_threshold.rb) | [Link](./test/medium/test_1343_number_of_sub_arrays_of_size_k_and_average_greater_than_or_equal_to_threshold.rb) |
685+
| 1347. Minimum Number of Steps to Make Two Strings Anagram | [Link](https://leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram/) | [Link](./lib/medium/1347_minimum_number_of_steps_to_make_two_strings_anagram.rb) | [Link](./test/medium/test_1347_minimum_number_of_steps_to_make_two_strings_anagram.rb) |
685686
| 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) |
686687
| 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) |
687688
| 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) |

leetcode-ruby.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ require 'English'
55
::Gem::Specification.new do |s|
66
s.required_ruby_version = '>= 3.0'
77
s.name = 'leetcode-ruby'
8-
s.version = '8.4.9'
8+
s.version = '8.5.0'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
1111
s.executable = 'leetcode-ruby'
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# frozen_string_literal: true
2+
3+
# https://leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram/
4+
# @param {String} s
5+
# @param {String} t
6+
# @return {Integer}
7+
def min_steps(s, t)
8+
s_letters = ::Array.new(128, 0)
9+
s.each_char { |c| s_letters[c.ord] += 1 }
10+
11+
t_letters = ::Array.new(128, 0)
12+
t.each_char { |c| t_letters[c.ord] += 1 }
13+
14+
result = 0
15+
16+
(0...s_letters.size).each do |i|
17+
s_count = s_letters[i]
18+
t_count = t_letters[i]
19+
20+
result += s_count - t_count if s_count > t_count
21+
end
22+
23+
result
24+
end
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../test_helper'
4+
require_relative '../../lib/medium/1347_minimum_number_of_steps_to_make_two_strings_anagram'
5+
require 'minitest/autorun'
6+
7+
class MinimumNumberOfStepsToMakeTwoStringsAnagramTest < ::Minitest::Test
8+
def test_default_one
9+
assert_equal(
10+
1,
11+
min_steps(
12+
'bab',
13+
'aba'
14+
)
15+
)
16+
end
17+
18+
def test_default_two
19+
assert_equal(
20+
5,
21+
min_steps(
22+
'leetcode',
23+
'practice'
24+
)
25+
)
26+
end
27+
28+
def test_default_three
29+
assert_equal(
30+
0,
31+
min_steps(
32+
'anagram',
33+
'mangaar'
34+
)
35+
)
36+
end
37+
end

0 commit comments

Comments
 (0)