Skip to content

Commit 93b8ac8

Browse files
authored
Merge pull request #802 from fartem/609-Find-Duplicate-File-in-System
2024-12-12 v. 7.3.1: added "609. Find Duplicate File in System"
2 parents 72ef7fa + 106d6bd commit 93b8ac8

File tree

4 files changed

+101
-1
lines changed

4 files changed

+101
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -595,3 +595,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
595595
| 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) |
596596
| 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) |
597597
| 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) |
598+
| 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) |

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 = '7.3.0'
8+
s.version = '7.3.1'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
1111
s.executable = 'leetcode-ruby'
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# frozen_string_literal: true
2+
3+
# https://leetcode.com/problems/find-duplicate-file-in-system/
4+
# @param {String[]} paths
5+
# @return {String[][]}
6+
def find_duplicate_paths(paths)
7+
map = {}
8+
paths.each do |path|
9+
values = path.split
10+
(1...values.size).each do |i|
11+
name = values[i].split('(')
12+
name[1] = name[1].gsub(/\)/, '')
13+
arr = map.fetch(name[1], [])
14+
arr << "#{values[0]}/#{name[0]}"
15+
map[name[1]] = arr
16+
end
17+
end
18+
19+
result = []
20+
map.each do |_, value|
21+
next unless value.size > 1
22+
23+
result << value
24+
end
25+
26+
result
27+
end
28+
29+
# Map<String, List<String>> map = new HashMap<>();
30+
# for (String path : paths) {
31+
# String[] values = path.split(" ");
32+
# for (int i = 1; i < values.length; i++) {
33+
# String[] name = values[i].split("\\(");
34+
# name[1] = name[1].replace(")", "");
35+
# List<String> list = map.getOrDefault(name[1], new ArrayList<>());
36+
# list.add(values[0] + "/" + name[0]);
37+
# map.put(name[1], list);
38+
# }
39+
# }
40+
# List<List<String>> result = new ArrayList<>();
41+
# for (String key : map.keySet()) {
42+
# if (map.get(key).size() > 1) {
43+
# result.add(map.get(key));
44+
# }
45+
# }
46+
# return result;
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../test_helper'
4+
require_relative '../../lib/medium/609_find_duplicate_file_in_system'
5+
require 'minitest/autorun'
6+
7+
class FindDuplicateFileInSystemTest < ::Minitest::Test
8+
def test_default_one
9+
assert_equal(
10+
[
11+
[
12+
'root/a/1.txt',
13+
'root/c/3.txt'
14+
],
15+
[
16+
'root/a/2.txt',
17+
'root/c/d/4.txt',
18+
'root/4.txt'
19+
]
20+
],
21+
find_duplicate_paths(
22+
[
23+
'root/a 1.txt(abcd) 2.txt(efgh)',
24+
'root/c 3.txt(abcd)',
25+
'root/c/d 4.txt(efgh)',
26+
'root 4.txt(efgh)'
27+
]
28+
)
29+
)
30+
end
31+
32+
def test_default_two
33+
assert_equal(
34+
[
35+
[
36+
'root/a/1.txt',
37+
'root/c/3.txt'
38+
],
39+
[
40+
'root/a/2.txt',
41+
'root/c/d/4.txt'
42+
]
43+
],
44+
find_duplicate_paths(
45+
[
46+
'root/a 1.txt(abcd) 2.txt(efgh)',
47+
'root/c 3.txt(abcd)',
48+
'root/c/d 4.txt(efgh)'
49+
]
50+
)
51+
)
52+
end
53+
end

0 commit comments

Comments
 (0)