MCPcopy Create free account
hub / github.com/Rohit91singh9/Coding-DP-DSA / countMaximumOperations

Method countMaximumOperations

countMaxOperations.java:3–36  ·  view source on GitHub ↗
(String s, String t)

Source from the content-addressed store, hash-verified

1// Approach 1
2
3public static int countMaximumOperations(String s, String t) {
4 // Write your code here
5 int sLength = s.length();
6
7 Map<Character, Integer> freqS = new HashMap<>();
8
9 for (int i = 0; i < sLength; i++) {
10 char current = s.charAt(i);
11
12 int count = freqS.getOrDefault(current, 0);
13 freqS.put(current, count + 1);
14 }
15
16 int tLength = t.length();
17 Map<Character, Integer> freqT = new HashMap<>();
18
19 for (int i = 0; i < tLength; i++) {
20 char current = t.charAt(i);
21
22 int count = freqT.getOrDefault(current, 0);
23 freqT.put(current, count + 1);
24 }
25
26 int result = Integer.MAX_VALUE;
27
28 for (char key : freqT.keySet()) {
29 int currentCount = freqS.getOrDefault(key, 0);
30 int requiredCount = freqT.get(key);
31
32 result = Math.min(result, currentCount / requiredCount);
33 }
34
35 return result == Integer.MAX_VALUE ? -1 : result;
36}
37
38
39// Approach 2

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected