Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
| 921. Minimum Add to Make Parentheses Valid | [Link](https://leetcode.com/problems/minimum-add-to-make-parentheses-valid/) | [Link](./lib/medium/921_minimum_add_to_make_parentheses_valid.rb) | [Link](./test/medium/test_921_minimum_add_to_make_parentheses_valid.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) |
| 2657. Find the Prefix Common Array of Two Arrays | [Link](https://leetcode.com/problems/find-the-prefix-common-array-of-two-arrays/) | [Link](./lib/medium/2657_find_the_prefix_common_array_of_two_arrays.rb) | [Link](./test/medium/test_2657_find_the_prefix_common_array_of_two_arrays.rb) |
| 3223. Minimum Length of String After Operations | [Link](https://leetcode.com/problems/minimum-length-of-string-after-operations/) | [Link](./lib/medium/3223_minimum_length_of_string_after_operations.rb) | [Link](./test/medium/test_3223_minimum_length_of_string_after_operations.rb) |

### Hard
Expand Down
2 changes: 1 addition & 1 deletion leetcode-ruby.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ require 'English'
::Gem::Specification.new do |s|
s.required_ruby_version = '>= 3.0'
s.name = 'leetcode-ruby'
s.version = '7.9.8'
s.version = '7.9.9'
s.license = 'MIT'
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
s.executable = 'leetcode-ruby'
Expand Down
24 changes: 24 additions & 0 deletions lib/medium/2657_find_the_prefix_common_array_of_two_arrays.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# frozen_string_literal: true

# https://leetcode.com/problems/find-the-prefix-common-array-of-two-arrays/
# @param {Integer[]} a
# @param {Integer[]} b
# @return {Integer[]}
def find_the_prefix_common_array(a, b)
n = a.size
freq = ::Array.new(n + 1, 0)
result = ::Array.new(n, 0)
common = 0

(0...n).each do |i|
freq[a[i]] += 1
common += 1 if freq[a[i]] == 2

freq[b[i]] += 1
common += 1 if freq[b[i]] == 2

result[i] = common
end

result
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# frozen_string_literal: true

require_relative '../test_helper'
require_relative '../../lib/medium/2657_find_the_prefix_common_array_of_two_arrays'
require 'minitest/autorun'

class FindThePrefixCommonArrayOfTwoArraysTest < ::Minitest::Test
def test_default_one
assert_equal(
[0, 2, 3, 4],
find_the_prefix_common_array(
[1, 3, 2, 4],
[3, 1, 2, 4]
)
)
end

def test_default_two
assert_equal(
[0, 1, 3],
find_the_prefix_common_array(
[2, 3, 1],
[3, 1, 2]
)
)
end
end
Loading