Skip to content

Commit 3da571a

Browse files
authored
Merge pull request eugenp#6052 from jainvarz/BAEL-2536-check-for-a-panagram-string
BAEL-2536-check-for-a-panagram-string
2 parents 1fdf586 + 082724a commit 3da571a

File tree

2 files changed

+104
-0
lines changed

2 files changed

+104
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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 Pangram {
10+
private static final int ALPHABET_COUNT = 26;
11+
12+
public static boolean isPangram(String str) {
13+
if (str == null)
14+
return false;
15+
Boolean[] alphabetMarker = new Boolean[ALPHABET_COUNT];
16+
Arrays.fill(alphabetMarker, false);
17+
int alphabetIndex = 0;
18+
String strUpper = str.toUpperCase();
19+
for (int i = 0; i < str.length(); i++) {
20+
if ('A' <= strUpper.charAt(i) && strUpper.charAt(i) <= 'Z') {
21+
alphabetIndex = strUpper.charAt(i) - 'A';
22+
alphabetMarker[alphabetIndex] = true;
23+
}
24+
}
25+
for (boolean index : alphabetMarker) {
26+
if (!index)
27+
return false;
28+
}
29+
return true;
30+
}
31+
32+
public static boolean isPangramWithStreams(String str) {
33+
if (str == null)
34+
return false;
35+
36+
// filtered character stream
37+
String strUpper = str.toUpperCase();
38+
Stream<Character> filteredCharStream = strUpper.chars()
39+
.filter(item -> ((item >= 'A' && item <= 'Z')))
40+
.mapToObj(c -> (char) c);
41+
Map<Character, Boolean> alphabetMap = filteredCharStream.collect(Collectors.toMap(item -> item, k -> Boolean.TRUE, (p1, p2) -> p1));
42+
43+
return (alphabetMap.size() == ALPHABET_COUNT);
44+
}
45+
46+
public static boolean isPerfectPangram(String str) {
47+
if (str == null)
48+
return false;
49+
50+
// filtered character stream
51+
String strUpper = str.toUpperCase();
52+
Stream<Character> filteredCharStream = strUpper.chars()
53+
.filter(item -> ((item >= 'A' && item <= 'Z')))
54+
.mapToObj(c -> (char) c);
55+
Map<Character, Long> alphabetFrequencyMap = filteredCharStream.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
56+
57+
return (alphabetFrequencyMap.size() == ALPHABET_COUNT && alphabetFrequencyMap.values()
58+
.stream()
59+
.allMatch(item -> item == 1));
60+
}
61+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.baeldung.string;
2+
3+
import static org.junit.Assert.assertFalse;
4+
import static org.junit.Assert.assertTrue;
5+
import org.junit.Test;
6+
7+
public class PangramUnitTest {
8+
9+
@Test
10+
public void givenValidString_isPangram_shouldReturnSuccess() {
11+
String input = "Two driven jocks help fax my big quiz";
12+
assertTrue(Pangram.isPangram(input));
13+
assertTrue(Pangram.isPangramWithStreams(input));
14+
}
15+
16+
@Test
17+
public void givenNullString_isPangram_shouldReturnFailure() {
18+
String input = null;
19+
assertFalse(Pangram.isPangram(input));
20+
assertFalse(Pangram.isPangramWithStreams(input));
21+
assertFalse(Pangram.isPerfectPangram(input));
22+
}
23+
24+
@Test
25+
public void givenPerfectPangramString_isPerfectPangram_shouldReturnSuccess() {
26+
String input = "abcdefghijklmNoPqrStuVwxyz";
27+
assertTrue(Pangram.isPerfectPangram(input));
28+
}
29+
30+
@Test
31+
public void givenNonPangramString_isPangram_shouldReturnFailure() {
32+
String input = "invalid pangram";
33+
assertFalse(Pangram.isPangram(input));
34+
assertFalse(Pangram.isPangramWithStreams(input));
35+
}
36+
37+
@Test
38+
public void givenPangram_isPerfectPangram_shouldReturnFailure() {
39+
String input = "Two driven jocks help fax my big quiz";
40+
assertFalse(Pangram.isPerfectPangram(input));
41+
}
42+
43+
}

0 commit comments

Comments
 (0)