-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem1400.java
More file actions
49 lines (48 loc) · 1.5 KB
/
Problem1400.java
File metadata and controls
49 lines (48 loc) · 1.5 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
import java.util.HashMap;
import java.util.Scanner;
public class Problem1400 {
// Construct K Palindrome Strings
public static void main(String[] args) {
Scanner obj=new Scanner(System.in);
int t=obj.nextInt();
while(t-->0){
obj.nextLine();
String str=obj.nextLine();
int k=obj.nextInt();
Solution1400 sc=new Solution1400();
if(sc.canConstruct(str, k)){
System.out.println("It is possible to construct K palindrome string using the given string.");
}else{
System.out.println("It is not possible to construct K palindrome string using the given string.");
}
}
obj.close();
}
}
class Solution1400{
public boolean canConstruct(String s,int k){
if(s.length()==k){
return true;
}else if(s.length()<k){
return false;
}else{
HashMap<Character,Integer> map=new HashMap<>();
for(char ch:s.toCharArray()){
map.put(ch, map.getOrDefault(ch, 0)+1);
}
int oddCount=0;
for(HashMap.Entry<Character,Integer> ele:map.entrySet()){
if(ele.getValue()%2!=0){
oddCount++;
}
}
if(map.size()==k){
return true;
}else if(oddCount==k || oddCount<k){
return true;
}else{
return false;
}
}
}
}