SHOW:
|
|
- or go back to the newest paste.
1 | // Author : Saurav Kalsoor | |
2 | - | // Count Quadruplets - JAVA |
2 | + | // K Unique String - JAVA |
3 | ||
4 | import java.util.*; | |
5 | ||
6 | public class Test { | |
7 | static Scanner sc = new Scanner(System.in); | |
8 | public static void main(String[] args) { | |
9 | - | int n = sc.nextInt(); |
9 | + | String s = sc.next(); |
10 | - | int m = sc.nextInt(); |
10 | + | int k = sc.nextInt(); |
11 | - | int p = sc.nextInt(); |
11 | + | |
12 | - | int q = sc.nextInt(); |
12 | + | System.out.println(kUniqueString(s, k)); |
13 | - | int target = sc.nextInt(); |
13 | + | |
14 | ||
15 | - | ArrayList<Integer> a = new ArrayList<>(); |
15 | + | public static int kUniqueString(String s, int k) { |
16 | - | ArrayList<Integer> b = new ArrayList<>(); |
16 | + | HashMap<Character, Integer> freq = new HashMap<>(); |
17 | - | ArrayList<Integer> c = new ArrayList<>(); |
17 | + | |
18 | - | ArrayList<Integer> d = new ArrayList<>(); |
18 | + | for(int i = 0; i < s.length(); i++){ |
19 | freq.put(s.charAt(i), freq.getOrDefault(s.charAt(i), 0) + 1); | |
20 | - | for(int i = 0;i < n; i++){ |
20 | + | |
21 | - | a.add(sc.nextInt()); |
21 | + | |
22 | Set<MyPair> st = new TreeSet<>(new Comparator<MyPair>() { | |
23 | @Override | |
24 | - | for(int i = 0;i < m; i++){ |
24 | + | public int compare(MyPair a, MyPair b) { |
25 | - | b.add(sc.nextInt()); |
25 | + | if(a.frequency == b.frequency) |
26 | return a.ch - b.ch; | |
27 | return a.frequency - b.frequency; | |
28 | - | for(int i = 0;i < p; i++){ |
28 | + | } |
29 | - | c.add(sc.nextInt()); |
29 | + | }); |
30 | ||
31 | for(Map.Entry<Character, Integer> x : freq.entrySet()){ | |
32 | - | for(int i = 0;i < q; i++){ |
32 | + | st.add(new MyPair(x.getKey(), x.getValue()) ); |
33 | - | d.add(sc.nextInt()); |
33 | + | } |
34 | ||
35 | - | System.out.println(countQuadruplets(a, b, c, d, target)); |
35 | + | int distinct = freq.size(), changes = 0; |
36 | ||
37 | for(MyPair x : st){ | |
38 | - | public static int countQuadruplets(ArrayList<Integer> a, ArrayList<Integer> b, ArrayList<Integer> c, ArrayList<Integer> d, int target) { |
38 | + | if(distinct <= k) |
39 | - | HashMap<Integer, Integer> map = new HashMap<>(); |
39 | + | break; |
40 | - | |
40 | + | |
41 | - | for (int i : a) { |
41 | + | changes += x.frequency; |
42 | - | for (int j : b) { |
42 | + | distinct--; |
43 | - | if (map.containsKey(i + j)) { |
43 | + | |
44 | - | map.put(i + j, map.get(i + j) + 1); |
44 | + | |
45 | - | } else |
45 | + | return changes; |
46 | - | map.put(i + j, 1); |
46 | + | |
47 | - | } |
47 | + | |
48 | - | } |
48 | + | |
49 | - | |
49 | + | |
50 | - | int count = 0; |
50 | + | class MyPair{ |
51 | - | |
51 | + | public char ch; |
52 | - | for (int i : c) { |
52 | + | public int frequency; |
53 | - | for (int j : d) { |
53 | + | |
54 | - | if (map.containsKey(target - i - j)) { |
54 | + | public MyPair(char ch, int frequency){ |
55 | - | count += map.get(target - i - j); |
55 | + | this.ch = ch; |
56 | - | } |
56 | + | this.frequency = frequency; |
57 | - | } |
57 | + | } |
58 | - | } |
58 | + | |
59 | - | |
59 | + |