-
Notifications
You must be signed in to change notification settings - Fork 0
/
Multiple.java
46 lines (45 loc) · 1.33 KB
/
Multiple.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
/*Question 2
Problem Statement –
Capgemini in its online written test have a coding question, wherein the students are given a string with multiple characters that are repeated consecutively. You’re supposed to reduce the size of this string using mathematical logic given as in the example below :
Input :
aabbbbeeeeffggg
Output:
a2b4e4f2g3
Input :
abbccccc
Output:
ab2c5 */
import java.util.*;
public class Multiple {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String a = sc.nextLine();
int i, j, count = 0;
int len = a.length();
String b = new String("");
for (i = 0; i < len; i++) {
count = 0;
for (j = 0; j <= i; j++) {
if (a.charAt(i) == a.charAt(j)) {
count++;
}
}
if (count == 1) {
b = b + a.charAt(i);
}
}
for (i = 0; i < b.length(); i++) {
count = 0;
for (j = 0; j < len; j++) {
if (b.charAt(i) == a.charAt(j)) {
count++;
}
}
if (count == 1) {
System.out.printf("%c", b.charAt(i));
} else {
System.out.printf("%c%d", b.charAt(i), count);
}
}
}
}