Skip to content

Commit 2721a82

Browse files
committed
added problem 609
1 parent d0d4666 commit 2721a82

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

problems/problem0609/file_dupes.go

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package problem0609
2+
3+
import (
4+
"strings"
5+
)
6+
7+
/*
8+
Given a list paths of directory info, including the directory path, and all the files with contents in this directory
9+
return all the duplicate files in the file system in terms of their paths. You may return the answer in any order.
10+
11+
A group of duplicate files consists of at least two files that have the same content.
12+
A single directory info string in the input list has the following format:
13+
"root/d1/d2/.../dm f1.txt(f1_content) f2.txt(f2_content) ... fn.txt(fn_content)"
14+
It means there are n files (f1.txt, f2.txt ... fn.txt) with content (f1_content, f2_content ... fn_content) respectively in the directory "root/d1/d2/.../dm". Note that n >= 1 and m >= 0. If m = 0, it means the directory is just the root directory.
15+
The output is a list of groups of duplicate file paths. For each group, it contains all the file paths of the files that have the same content. A file path is a string that has the following format:
16+
"directory_path/file_name.txt"
17+
*/
18+
19+
func findDuplicate(paths []string) [][]string {
20+
var result = make([][]string, 0)
21+
var contentMap = map[string][]string{}
22+
for _, path := range paths {
23+
split := strings.Split(path, " ")
24+
for _, file := range split[1:] {
25+
content_sep := strings.Index(file, "(")
26+
name := file[:content_sep]
27+
content := file[content_sep+1 : len(file)-1]
28+
contentMap[content] = append(contentMap[content], split[0]+"/"+name)
29+
}
30+
}
31+
for k := range contentMap {
32+
if len(contentMap[k]) > 1 {
33+
result = append(result, contentMap[k])
34+
}
35+
}
36+
return result
37+
}
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package problem0609
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
type Result struct {
11+
Input []string
12+
Expected [][]string
13+
}
14+
15+
var Results = []Result{
16+
{[]string{"root/a 1.txt(abcd) 2.txt(efgh)", "root/c 3.txt(abcd)", "root/c/d 4.txt(efgh)", "root 4.txt(efgh)"},
17+
[][]string{{"root/a/1.txt", "root/c/3.txt"}, {"root/a/2.txt", "root/c/d/4.txt", "root/4.txt"}}},
18+
{[]string{"root/a 1.txt(abcd) 2.txt(efgh)", "root/c 3.txt(abcd)", "root/c/d 4.txt(efgh)"},
19+
[][]string{{"root/a/1.txt", "root/c/3.txt"}, {"root/a/2.txt", "root/c/d/4.txt"}}},
20+
}
21+
22+
func TestFileDuplicates(t *testing.T) {
23+
assert := assert.New(t)
24+
25+
for _, res := range Results {
26+
want := res.Expected
27+
got := findDuplicate(res.Input)
28+
assert.Equal(want, got, fmt.Sprintf("%+v", res))
29+
}
30+
}

readme.md

+1
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ Each problem is in it's own directory, with test files. There are helper package
112112
| 0576 | [Out of Boundary Paths](https://leetcode.com/problems/out-of-boundary-paths) | [My Solution](./problems/problem0576) ||
113113
| 0583 | [Delete Operation for Two Strings](https://leetcode.com/problems/delete-operation-for-two-strings) | [My Solution](./problems/problem0583) ||
114114
| 0606 | [Construct String from Binary Tree](https://leetcode.com/problems/construct-string-from-binary-tree) | [My Solution](./problems/problem0606) ||
115+
| 0609 | [Find Duplicate File in System](https://leetcode.com/problems/find-duplicate-file-in-system) | [My Solution](./problems/problem0609) ||
115116
| 0617 | [Merge Two Binary Trees](https://leetcode.com/problems/merge-two-binary-trees) | [My Solution](./problems/problem00617) ||
116117
| 0630 | [Course Schedule III](https://leetcode.com/problems/course-schedule-iii) | [My Solution](./problems/problem0630) ||
117118
| 0637 | [Average of Levels in Binary Tree](https://leetcode.com/problems/average-of-levels-in-binary-tree) | [My Solution](./problems/problem0637) ||

0 commit comments

Comments
 (0)