Advertisement
LeatherDeer

Untitled

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