-
Notifications
You must be signed in to change notification settings - Fork 1
/
InfyStr.java
executable file
·53 lines (43 loc) · 1.14 KB
/
InfyStr.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
import java.util.*;
class InfyStr{
public static void main(String[] args){
Scanner s= new Scanner(System.in);
// String in=s.nextLine();
String in="99 aa";
System.out.print(solve(in));
}
static String solve(String in){
int spcl;
int maxChar=getMaxFreq(in, "s");
int maxDigit=getMaxFreq(in,"d");
spcl=Math.abs(maxChar-maxDigit);
if(!(in.charAt(spcl)==' ')){
String rchar=in.substring(spcl,spcl+1);
in=in.replace(rchar, "");
}
in=in.replace(' ', '$');
return in;
}
static int getMaxFreq(String s, String exception){
int maxFreq = -1;
int count[] = new int[256];
for (int i=0; i<s.length(); i++){
char curr=s.charAt(i);
if(exception.equals("s")){
if(Character.isLetter(curr))
count[curr]++;
}
else if(exception.equals("d")){
if(Character.isDigit(curr)){
count[curr]++;
}
}
}
for (int i = 0; i < s.length(); i++) {
if (maxFreq < count[s.charAt(i)]) {
maxFreq = count[s.charAt(i)];
}
}
return maxFreq;
}
}