|
| 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 | +} |
0 commit comments