-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem3223.java
More file actions
56 lines (55 loc) · 1.63 KB
/
Problem3223.java
File metadata and controls
56 lines (55 loc) · 1.63 KB
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
import java.util.HashMap;
import java.util.Scanner;
public class Problem3223{
// Minimum Length of String After Operation
public static void main(String[] args) {
Scanner obj=new Scanner(System.in);
int t=obj.nextInt();
obj.nextLine();
while(t-->0){
String str=obj.nextLine();
Solution3223A sc=new Solution3223A();
System.out.println(sc.minimumLength(str));
Solution3223B scb=new Solution3223B();
System.out.println(scb.minimumLength(str));
}
obj.close();
}
}
class Solution3223A{
public int minimumLength(String s){
int count=0;
HashMap<Character,Integer> map=new HashMap<>();
for(char ch:s.toCharArray()){
map.put(ch, map.getOrDefault(ch, 0)+1);
}
for(HashMap.Entry<Character,Integer> ele:map.entrySet()){
int x=ele.getValue();
if (x%2!=0)
map.put(ele.getKey(), 1);
else
map.put(ele.getKey(), 2);
}
for(HashMap.Entry<Character,Integer> ele:map.entrySet()){
count+=ele.getValue();
}
return count;
}
}
class Solution3223B{
public int minimumLength(String s){
int count=0;
HashMap<Character,Integer> map=new HashMap<>();
for(char ch:s.toCharArray()){
map.put(ch, map.getOrDefault(ch, 0)+1);
}
for(HashMap.Entry<Character,Integer> ele:map.entrySet()){
int x=ele.getValue();
if (x%2!=0)
count+=1;
else
count+=2;
}
return count;
}
}