-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathFormPalindrome.java
More file actions
33 lines (30 loc) · 940 Bytes
/
FormPalindrome.java
File metadata and controls
33 lines (30 loc) · 940 Bytes
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
import java.util.HashMap;
import java.util.Map;
public class FormPalindrome {
public static void main(String[] args) {
String str = "carrace";
int oddCount = 0;
HashMap<Character, Integer> map = new HashMap<>();
for(int i= 0 ; i<str.length(); i++){
char singleChar = str.charAt(i);
if(map.get(singleChar)==null){
map.put(singleChar, 1);
}
else{
int count = map.get(singleChar);
count++;
map.put(singleChar, count);
}
}
for(Map.Entry<Character,Integer> m : map.entrySet()){
if(m.getValue()%2!=0){
oddCount++;
if(oddCount>1){
System.out.println("Not Form a Palindrome");
return ;
}
}
}
System.out.println("Form a Palindrome");
}
}