File tree 1 file changed +34
-1
lines changed
find_duplicate_file_in_system/src
1 file changed +34
-1
lines changed Original file line number Diff line number Diff line change
1
+ use std:: collections:: HashMap ;
2
+
1
3
pub struct Solution { }
2
4
3
5
impl Solution {
6
+ fn parse_path ( path : & str ) -> Vec < ( String , String ) > {
7
+ let mut root = "" ;
8
+ let mut result = vec ! [ ] ;
9
+ for ( i, s) in path. split ( ' ' ) . enumerate ( ) {
10
+ if i == 0 {
11
+ root = s;
12
+ } else {
13
+ let left = s. find ( "(" ) . unwrap ( ) ;
14
+ result. push ( (
15
+ format ! ( "{}/{}" , root, & s[ ..left] ) ,
16
+ s[ left + 1 ..s. len ( ) - 1 ] . to_string ( ) ,
17
+ ) )
18
+ }
19
+ }
20
+ result
21
+ }
22
+
4
23
pub fn find_duplicate ( paths : Vec < String > ) -> Vec < Vec < String > > {
5
- vec ! [ vec![ ] ]
24
+ let mut map = HashMap :: new ( ) ;
25
+ for path in paths. iter ( ) {
26
+ for ( p, content) in Self :: parse_path ( path) {
27
+ match map. get_mut ( & content) {
28
+ None => {
29
+ map. insert ( content, vec ! [ p] ) ;
30
+ }
31
+ Some ( v) => v. push ( p) ,
32
+ }
33
+ }
34
+ }
35
+ map. iter ( )
36
+ . filter ( |( _, v) | v. len ( ) > 1 )
37
+ . map ( |( _, v) | v. clone ( ) )
38
+ . collect :: < Vec < Vec < String > > > ( )
6
39
}
7
40
}
8
41
You can’t perform that action at this time.
0 commit comments