-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPalindromePermutationChecker.java
More file actions
31 lines (24 loc) · 1.09 KB
/
Copy pathPalindromePermutationChecker.java
File metadata and controls
31 lines (24 loc) · 1.09 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
import java.util.HashMap;
import java.util.Map;
public class PalindromePermutationChecker {
public static boolean canPermutePalindrome(String s) {
// Map to store character counts
Map<Character, Integer> charCounts = new HashMap<>();
// Count each character
for (char c : s.toCharArray()) {
charCounts.put(c, charCounts.getOrDefault(c, 0) + 1);
}
// Count the number of characters that have an odd count using a stream
long oddCount = charCounts.values().stream()
.filter(count -> count % 2 != 0)
.count();
// A string can form a palindrome if there is at most one odd count
return oddCount <= 1;
}
public static void main(String[] args) {
String input1 = "tactcoa";
String input2 = "java";
System.out.println("Can \"" + input1 + "\" permute to a palindrome? " + canPermutePalindrome(input1));
System.out.println("Can \"" + input2 + "\" permute to a palindrome? " + canPermutePalindrome(input2));
}
}