美文网首页
Find Duplicate File in System

Find Duplicate File in System

作者: Frank_Kivi | 来源:发表于2018-06-28 10:19 被阅读6次

https://www.lintcode.com/problem/find-duplicate-file-in-system/description

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class Solution {
    /**
     * @param paths: a list of string
     * @return: all the groups of duplicate files in the file system in terms of their paths
     */
    public List<List<String>> findDuplicate(String[] paths) {
        // Write your code here
        Map<String, List<String>> map = new HashMap<>();
        for (int i = 0; i < paths.length; i++) {
            String path = paths[i];
            String[] split = path.split(" ");
            for (int j = 1; j < split.length; j++) {
                String s = split[j];
                int index = s.indexOf("(");
                String fileName = s.substring(0, index);
                String content = s.substring(index + 1, s.length() - 1);
                List<String> list = map.get(content);
                if (list == null) {
                    list = new ArrayList<>();
                    map.put(content, list);
                }
                list.add(split[0] + "/" + fileName);
            }
        }
        List<List<String>> result = new ArrayList<>();
        Iterator<Map.Entry<String, List<String>>> iterator = map.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, List<String>> next = iterator.next();
            if (next.getValue().size() >= 2) {
                result.add(next.getValue());
            }
        }
        return result;
    }
}

相关文章

网友评论

      本文标题:Find Duplicate File in System

      本文链接:https://www.haomeiwen.com/subject/ygtgyftx.html