Skip to content

Commit 75bf7ba

Browse files
committed
BAEL-2536-check-for-a-panagram-string
1 parent 9ffbe2b commit 75bf7ba

1 file changed

Lines changed: 60 additions & 0 deletions

File tree

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.baeldung.string;
2+
3+
import java.util.Arrays;
4+
import java.util.Map;
5+
import java.util.function.Function;
6+
import java.util.stream.Collectors;
7+
import java.util.stream.Stream;
8+
9+
public class Panagram {
10+
11+
public static boolean isPanagram(String str) {
12+
if (str == null)
13+
return false;
14+
Boolean[] alphabetMarker = new Boolean[26];
15+
Arrays.fill(alphabetMarker, false);
16+
int alphabetIndex = 0;
17+
str = str.toUpperCase();
18+
for (int i = 0; i < str.length(); i++) {
19+
if ('A' <= str.charAt(i) && str.charAt(i) <= 'Z') {
20+
alphabetIndex = str.charAt(i) - 'A';
21+
alphabetMarker[alphabetIndex] = true;
22+
}
23+
}
24+
for (boolean index : alphabetMarker) {
25+
if (!index)
26+
return false;
27+
}
28+
return true;
29+
}
30+
31+
public static boolean isPanagramWithStreams(String str) {
32+
if (str == null)
33+
return false;
34+
35+
// filtered character stream
36+
str = str.toUpperCase();
37+
Stream<Character> filteredCharStream = str.chars()
38+
.filter(item -> ((item >= 'A' && item <= 'Z') || (item >= 'a' && item <= 'z')))
39+
.mapToObj(c -> (char) c);
40+
Map<Character, Boolean> alphabetMap = filteredCharStream.collect(Collectors.toMap(item -> item, k -> Boolean.TRUE, (p1, p2) -> p1));
41+
42+
return (alphabetMap.size() == 26);
43+
}
44+
45+
public static boolean isPerfectPanagram(String str) {
46+
if (str == null)
47+
return false;
48+
49+
// filtered character stream
50+
str = str.toUpperCase();
51+
Stream<Character> filteredCharStream = str.chars()
52+
.filter(item -> ((item >= 'A' && item <= 'Z') || (item >= 'a' && item <= 'z')))
53+
.mapToObj(c -> (char) c);
54+
Map<Character, Long> alphabetFrequencyMap = filteredCharStream.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
55+
56+
return (alphabetFrequencyMap.size() == 26 && alphabetFrequencyMap.values()
57+
.stream()
58+
.allMatch(item -> item == 1));
59+
}
60+
}

0 commit comments

Comments
 (0)