Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. import java.util.HashMap;
  2. import java.util.Map;
  3. import java.util.Scanner;
  4.  
  5. public class Solution {
  6. Map<String, Integer> magazineMap;
  7. Map<String, Integer> noteMap;
  8.  
  9. public Solution(String magazine, String note) {
  10. this.noteMap = new HashMap<String, Integer>();
  11. this.magazineMap = new HashMap<String, Integer>();
  12.  
  13. // Must use an object instead of a primitive because it may be assigned a null reference.
  14. Integer occurrences;
  15.  
  16. for (String s : magazine.split("[^a-zA-Z]+")) {
  17. occurrences = magazineMap.get(s);
  18.  
  19. if (occurrences == null) {
  20. magazineMap.put(s, 1);
  21. } else {
  22. magazineMap.put(s, occurrences + 1);
  23. }
  24. }
  25.  
  26. for (String s : note.split("[^a-zA-Z]+")) {
  27. occurrences = noteMap.get(s);
  28.  
  29. if (occurrences == null) {
  30. noteMap.put(s, 1);
  31. } else {
  32. noteMap.put(s, occurrences + 1);
  33. }
  34. }
  35.  
  36. }
  37.  
  38. public void solve() {
  39. boolean canReplicate = true;
  40. for (String s : noteMap.keySet()) {
  41. if (!magazineMap.containsKey(s) || (magazineMap.get(s) < noteMap.get(s))) {
  42. canReplicate = false;
  43. break;
  44. }
  45. }
  46.  
  47. System.out.println((canReplicate) ? "Yes" : "No");
  48. }
  49.  
  50. public static void main(String[] args) {
  51. Scanner scanner = new Scanner(System.in);
  52. int n = scanner.nextInt();
  53. int m = scanner.nextInt();
  54.  
  55. // Eat whitespace to beginning of next line
  56. scanner.nextLine();
  57.  
  58. Solution s = new Solution(scanner.nextLine(), scanner.nextLine());
  59. scanner.close();
  60.  
  61. s.solve();
  62. }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement