-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathCount_Elimination_Character.java
68 lines (62 loc) · 1.65 KB
/
Count_Elimination_Character.java
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
/* Find how many characters must be removed in a string of a character series to get a common difference of the count
of different characters. If no characters need to remove then print "NA".
Input
----------------
aaaaabbc
1
Output
----------------
2
Process
----------------
Number of a : 5
Number of b : 2
Number of c : 1
Common difference : 1
5(a) - 2(b) = 3(extra 3 a are there), so we should remove 3-1= 2(a)
2(b) - 1(c) = 1, no charactres should remove
Hence the output = 2.
*/
import java.util.*;
public class Count_Elimination_Character
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
int i=0, j = 0;
int[] arr = new int[0];
while(i == j) {
int count = 0;
while(j < s.length()) {
if(s.charAt(i) == s.charAt(j))
{
count++;
}
else
{
arr = Arrays.copyOf(arr, arr.length+1);
arr[arr.length-1] = count;
i = j;
break;
}
j++;
}
if(j==s.length())
{
arr = Arrays.copyOf(arr, arr.length+1);
arr[arr.length-1] = count;
break;
}
}
int diff = sc.nextInt();
int c = 0;
for (int k = arr.length-1; k >0; k--) {
if(arr[k-1] - arr[k] != diff)
{
c += arr[k-1] - arr[k] - diff;
arr[k - 1] = arr[k] + diff;
}
}
System.out.println(c);
}
}