Advertisement
LeatherDeer

DZ_5

Jan 13th, 2023
1,004
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.78 KB | None | 0 0
  1. class Solution {
  2.     public List<List<String>> findDuplicate(String[] paths) {
  3.         // content - paths
  4.         Map<String, List<String>> map = new HashMap<>();
  5.         for (var path : paths) {
  6.             String[] info = path.split(" ");
  7.             String dir = info[0];
  8.             for (int i = 1; i < info.length; i++) {
  9.                 String file = info[i];
  10.                 int start = file.indexOf('(');
  11.                 int end = file.length();
  12.                
  13.                 String content = file.substring(start + 1, end);
  14.                 String filePath = dir + "/" + file.substring(0, start);
  15.                
  16.                 map.putIfAbsent(content, new ArrayList<>());
  17.                 map.get(content).add(filePath);
  18.             }
  19.         }
  20.  
  21.  
  22.         List<List<String>> ans = new ArrayList<>();
  23.         for (var e : map.entrySet()) {
  24.             if (e.getValue().size() > 1) {
  25.                 ans.add(e.getValue());
  26.             }
  27.         }
  28.         return ans;
  29.     }
  30. }
  31.  
  32. class Solution {
  33.     public int countWords(String[] words1, String[] words2) {
  34.         if (words1.length > words2.length) {
  35.             return countWords(words2, words1);
  36.         }
  37.  
  38.         Map<String, Integer> map = new HashMap<>();
  39.         for (int i = 0; i < words1.length; i++) {
  40.             map.putIfAbsent(words1[i], 0);
  41.             map.put(words1[i], map.get(words1[i]) + 1);
  42.         }
  43.  
  44.         int ans = 0;
  45.         for (int i = 0; i < words2.length; i++) {
  46.             if (map.containsKey(words2[i]) && map.get(words2[i]) < 2) {
  47.                 map.put(words2[i], map.get(words2[i]) - 1);
  48.             }
  49.         }
  50.  
  51.         for (var e : map.entrySet()) {
  52.             if (e.getValue() == 0) {
  53.                 ans++;
  54.             }
  55.         }
  56.  
  57.         return ans;
  58.     }
  59. }
  60.  
  61.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement