@@ -52,32 +52,34 @@ If the file content is very large (GB level), how will you modify your solution?
5252 */
5353public class _609 {
5454
55- public List <List <String >> findDuplicate (String [] paths ) {
56- Map <String , List <String >> map = new HashMap <>();//key is the file content, value is the list of directories that has this directory/file
57- for (String path : paths ) {
58- String [] dirAndFiles = path .split (" " );
59- for (int i = 1 ; i < dirAndFiles .length ; i ++) {
60- String content = dirAndFiles [i ].substring (dirAndFiles [i ].indexOf ("(" ) + 1 , dirAndFiles [i ].indexOf (")" ));
61- if (!map .containsKey (content )) {
62- map .put (content , new ArrayList <>());
55+ public static class Solution1 {
56+ public List <List <String >> findDuplicate (String [] paths ) {
57+ Map <String , List <String >> map = new HashMap <>();//key is the file content, value is the list of directories that has this directory/file
58+ for (String path : paths ) {
59+ String [] dirAndFiles = path .split (" " );
60+ for (int i = 1 ; i < dirAndFiles .length ; i ++) {
61+ String content = dirAndFiles [i ].substring (dirAndFiles [i ].indexOf ("(" ) + 1 , dirAndFiles [i ].indexOf (")" ));
62+ if (!map .containsKey (content )) {
63+ map .put (content , new ArrayList <>());
64+ }
65+ List <String > dirs = map .get (content );
66+ dirs .add (dirAndFiles [0 ] + "/" + dirAndFiles [i ].substring (0 , dirAndFiles [i ].indexOf ("(" )));
67+ map .put (content , dirs );
6368 }
64- List <String > dirs = map .get (content );
65- dirs .add (dirAndFiles [0 ] + "/" + dirAndFiles [i ].substring (0 , dirAndFiles [i ].indexOf ("(" )));
66- map .put (content , dirs );
6769 }
68- }
6970
70- List <List <String >> result = new ArrayList <>();
71- for (String content : map .keySet ()) {
72- if (map .get (content ).size () > 1 ) {
73- List <String > dupFile = new ArrayList <>();
74- for (String dir : map .get (content )) {
75- dupFile .add (dir );
71+ List <List <String >> result = new ArrayList <>();
72+ for (String content : map .keySet ()) {
73+ if (map .get (content ).size () > 1 ) {
74+ List <String > dupFile = new ArrayList <>();
75+ for (String dir : map .get (content )) {
76+ dupFile .add (dir );
77+ }
78+ result .add (dupFile );
7679 }
77- result .add (dupFile );
7880 }
81+ return result ;
7982 }
80- return result ;
8183 }
8284
8385 /**Answers to follow-up questions:
0 commit comments