diff --git a/README.md b/README.md index 70df239a..5e1d0488 100644 --- a/README.md +++ b/README.md @@ -595,3 +595,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/). | 560. Subarray Sum Equals K | [Link](https://leetcode.com/problems/subarray-sum-equals-k/) | [Link](./lib/medium/560_subarray_sum_equals_k.rb) | [Link](./test/medium/test_560_subarray_sum_equals_k.rb) | | 581. Shortest Unsorted Continuous Subarray | [Link](https://leetcode.com/problems/shortest-unsorted-continuous-subarray/) | [Link](./lib/medium/581_shortest_unsorted_continuous_subarray.rb) | [Link](./test/medium/test_581_shortest_unsorted_continuous_subarray.rb) | | 606. Construct String from Binary Tree | [Link](https://leetcode.com/problems/construct-string-from-binary-tree/) | [Link](./lib/medium/606_construct_string_from_binary_tree.rb) | [Link](./test/medium/test_606_construct_string_from_binary_tree.rb) | +| 609. Find Duplicate File in System | [Link](https://leetcode.com/problems/find-duplicate-file-in-system/) | [Link](./lib/medium/609_find_duplicate_file_in_system.rb) | [Link](./test/medium/test_609_find_duplicate_file_in_system.rb) | diff --git a/leetcode-ruby.gemspec b/leetcode-ruby.gemspec index 48912c2f..e9807e63 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 = '7.3.0' + s.version = '7.3.1' s.license = 'MIT' s.files = ::Dir['lib/**/*.rb'] + %w[README.md] s.executable = 'leetcode-ruby' diff --git a/lib/medium/609_find_duplicate_file_in_system.rb b/lib/medium/609_find_duplicate_file_in_system.rb new file mode 100644 index 00000000..397c89fc --- /dev/null +++ b/lib/medium/609_find_duplicate_file_in_system.rb @@ -0,0 +1,46 @@ +# frozen_string_literal: true + +# https://leetcode.com/problems/find-duplicate-file-in-system/ +# @param {String[]} paths +# @return {String[][]} +def find_duplicate_paths(paths) + map = {} + paths.each do |path| + values = path.split + (1...values.size).each do |i| + name = values[i].split('(') + name[1] = name[1].gsub(/\)/, '') + arr = map.fetch(name[1], []) + arr << "#{values[0]}/#{name[0]}" + map[name[1]] = arr + end + end + + result = [] + map.each do |_, value| + next unless value.size > 1 + + result << value + end + + result +end + +# Map> map = new HashMap<>(); +# for (String path : paths) { +# String[] values = path.split(" "); +# for (int i = 1; i < values.length; i++) { +# String[] name = values[i].split("\\("); +# name[1] = name[1].replace(")", ""); +# List list = map.getOrDefault(name[1], new ArrayList<>()); +# list.add(values[0] + "/" + name[0]); +# map.put(name[1], list); +# } +# } +# List> result = new ArrayList<>(); +# for (String key : map.keySet()) { +# if (map.get(key).size() > 1) { +# result.add(map.get(key)); +# } +# } +# return result; diff --git a/test/medium/test_609_find_duplicate_file_in_system.rb b/test/medium/test_609_find_duplicate_file_in_system.rb new file mode 100644 index 00000000..924a20f4 --- /dev/null +++ b/test/medium/test_609_find_duplicate_file_in_system.rb @@ -0,0 +1,53 @@ +# frozen_string_literal: true + +require_relative '../test_helper' +require_relative '../../lib/medium/609_find_duplicate_file_in_system' +require 'minitest/autorun' + +class FindDuplicateFileInSystemTest < ::Minitest::Test + def test_default_one + assert_equal( + [ + [ + 'root/a/1.txt', + 'root/c/3.txt' + ], + [ + 'root/a/2.txt', + 'root/c/d/4.txt', + 'root/4.txt' + ] + ], + find_duplicate_paths( + [ + 'root/a 1.txt(abcd) 2.txt(efgh)', + 'root/c 3.txt(abcd)', + 'root/c/d 4.txt(efgh)', + 'root 4.txt(efgh)' + ] + ) + ) + end + + def test_default_two + assert_equal( + [ + [ + 'root/a/1.txt', + 'root/c/3.txt' + ], + [ + 'root/a/2.txt', + 'root/c/d/4.txt' + ] + ], + find_duplicate_paths( + [ + 'root/a 1.txt(abcd) 2.txt(efgh)', + 'root/c 3.txt(abcd)', + 'root/c/d 4.txt(efgh)' + ] + ) + ) + end +end