609.find-duplicate-file-in-system.java 962 B

1234567891011121314151617181920212223242526
  1. class Solution {
  2. public List<List<String>> findDuplicate(String[] paths) {
  3. List<List<String>> res = new LinkedList<>();
  4. HashMap<String, Integer> map = new HashMap<>();
  5. for (String path : paths) {
  6. String[] part = path.split(" ");
  7. for (int i = 1; i < part.length; i++) {
  8. String[] file = part[i].split("\\(");
  9. Integer idx = map.get(file[1]);
  10. if (idx == null) {
  11. res.add(new LinkedList<String>());
  12. idx = res.size() - 1;
  13. map.put(file[1], idx);
  14. }
  15. String fullPath = part[0] + "/" + file[0];
  16. res.get(idx).add(fullPath);
  17. }
  18. }
  19. Iterator<List<String>> iter = res.iterator();
  20. while (iter.hasNext()) {
  21. List<String> list = iter.next();
  22. if (list.size() == 1) iter.remove();
  23. }
  24. return res;
  25. }
  26. }