Advertisement
LeatherDeer

Untitled

Dec 21st, 2022
921
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.93 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.                 String content = file.substring(start + 1, end);
  13.  
  14.                 map.putIfAbsent(content, new ArrayList<>());
  15.                 map.get(content).add(dir + "/" + file.substring(0, start));
  16.             }
  17.         }
  18.  
  19.  
  20.         List<List<String>> ans = new ArrayList<>();
  21.         for (var e : map.entrySet()) {
  22.             if (e.getValue().size() > 1) {
  23.                 ans.add(e.getValue());
  24.             }
  25.         }
  26.         return ans;
  27.     }
  28. }
  29.  
  30.  
  31. Close
  32. umpkf1234
  33. umpkf1234
  34. Nov 05, 2022 22:03
  35.  
  36. Details
  37. Solution
  38. Java
  39. Runtime
  40. 9 ms
  41. Beats
  42. 72.82%
  43. Memory
  44. 45.9 MB
  45. Beats
  46. 55.57%
  47. Click to check the distribution chart
  48. Notes
  49. Write your notes here
  50. Related Tags
  51. Select tags
  52. 0/5
  53. class Solution {
  54.     public int countWords(String[] words1, String[] words2) {
  55.         if (words1.length > words2.length) {
  56.             return countWords(words2, words1);
  57.         }
  58.  
  59.         Map<String, Integer> map = new HashMap<>();
  60.         for (int i = 0; i < words1.length; i++) {
  61.             map.putIfAbsent(words1[i], 0);
  62.             map.put(words1[i], map.get(words1[i]) + 1);
  63.         }
  64.  
  65.         int ans = 0;
  66.         for (int i = 0; i < words2.length; i++) {
  67.             if (map.containsKey(words2[i]) && map.get(words2[i]) < 2) {
  68.                 map.put(words2[i], map.get(words2[i]) - 1);
  69.             }
  70.         }
  71.  
  72.         for (var e : map.entrySet()) {
  73.             if (e.getValue() == 0) {
  74.                 ans++;
  75.             }
  76.         }
  77.  
  78.         return ans;
  79.     }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement