-
Notifications
You must be signed in to change notification settings - Fork 126
Expand file tree
/
Copy pathcountMaxOperations.java
More file actions
87 lines (57 loc) · 2.23 KB
/
countMaxOperations.java
File metadata and controls
87 lines (57 loc) · 2.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Approach 1
public static int countMaximumOperations(String s, String t) {
// Write your code here
int sLength = s.length();
Map<Character, Integer> freqS = new HashMap<>();
for (int i = 0; i < sLength; i++) {
char current = s.charAt(i);
int count = freqS.getOrDefault(current, 0);
freqS.put(current, count + 1);
}
int tLength = t.length();
Map<Character, Integer> freqT = new HashMap<>();
for (int i = 0; i < tLength; i++) {
char current = t.charAt(i);
int count = freqT.getOrDefault(current, 0);
freqT.put(current, count + 1);
}
int result = Integer.MAX_VALUE;
for (char key : freqT.keySet()) {
int currentCount = freqS.getOrDefault(key, 0);
int requiredCount = freqT.get(key);
result = Math.min(result, currentCount / requiredCount);
}
return result == Integer.MAX_VALUE ? -1 : result;
}
// Approach 2
/*
public static int countMaxOperations(String s, String t) {
int sLength = s.length();
Map<Character, Integer> freqS = new HashMap<>();
for (int i = 0; i < sLength; i++) {
char current = s.charAt(i);
int count = freqS.getOrDefault(current, 0);
freqS.put(current, count + 1);
}
int tLength = t.length();
Map<Character, Integer> freqT = new HashMap<>();
for (int i = 0; i < tLength; i++) {
char current = t.charAt(i);
int count = freqT.getOrDefault(current, 0);
freqT.put(current, count + 1);
}
int result = Integer.MAX_VALUE;
for (char key : freqT.keySet()) {
int currentCount = freqS.getOrDefault(key, 0);
int requiredCount = freqT.get(key);
result = Math.min(result, currentCount / requiredCount);
}
return result == Integer.MAX_VALUE ? -1 : result;
}
*/
/* NOTE:
I completed this Assessment during Amazon Hackerrank 1st Round
If you can figure out any other Approach to solve this problem, I would love to hear
from you on the Mail ID I have Provided. if you've cracked the Interview.
beingactual@gmail.com
*/