Skip to content

Commit bb8cb72

Browse files
Update Pangram.java
1 parent 6d0d701 commit bb8cb72

File tree

1 file changed

+28
-95
lines changed

1 file changed

+28
-95
lines changed
Lines changed: 28 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1,103 +1,36 @@
1-
package com.thealgorithms.strings;
2-
3-
import java.util.HashSet;
4-
5-
/**
6-
* Checks if a given string is a Pangram (a sentence containing every letter of the alphabet at least once).
7-
* Wikipedia: https://en.wikipedia.org/wiki/Pangram
8-
*/
9-
public final class Pangram {
10-
11-
private Pangram() {
12-
}
13-
14-
/**
15-
* Main method to test all pangram-checking methods.
16-
*/
17-
public static void main(String[] args) {
18-
assert isPangram("The quick brown fox jumps over the lazy dog");
19-
assert !isPangram("The quick brown fox jumps over the azy dog"); // L is missing
20-
assert !isPangram("+-1234 This string is not alphabetical");
21-
assert !isPangram("\u0000/\\");
22-
assert isPangramUsingBitwise("The quick brown fox jumps over the lazy dog");
23-
assert !isPangramUsingBitwise("Hello, World!");
24-
}
25-
26-
/**
27-
* Checks if a String is a Pangram using a HashSet to track unique characters.
28-
*
29-
* @param s The String to check
30-
* @return {@code true} if s is a Pangram, otherwise {@code false}
31-
*/
32-
public static boolean isPangramUsingSet(String s) {
33-
HashSet<Character> uniqueChars = new HashSet<>();
34-
s = s.toLowerCase();
35-
for (char c : s.toCharArray()) {
36-
if (c >= 'a' && c <= 'z') {
37-
uniqueChars.add(c);
38-
}
39-
}
40-
return uniqueChars.size() == 26;
1+
import org.junit.jupiter.api.Test;
2+
import static org.junit.jupiter.api.Assertions.assertTrue;
3+
import static org.junit.jupiter.api.Assertions.assertFalse;
4+
5+
public class PangramTest {
6+
7+
@Test
8+
public void testIsPangram() {
9+
assertTrue(Pangram.isPangram("The quick brown fox jumps over the lazy dog"));
10+
assertFalse(Pangram.isPangram("The quick brown fox jumps over the azy dog")); // L is missing
11+
assertFalse(Pangram.isPangram("+-1234 This string is not alphabetical"));
12+
assertFalse(Pangram.isPangram("\u0000/\\"));
4113
}
4214

43-
/**
44-
* Checks if a String is a Pangram by tracking occurrences of each letter in an array.
45-
*
46-
* @param s The String to check
47-
* @return {@code true} if s is a Pangram, otherwise {@code false}
48-
*/
49-
public static boolean isPangram(String s) {
50-
boolean[] lettersExisting = new boolean[26];
51-
s = s.toLowerCase();
52-
for (char c : s.toCharArray()) {
53-
if (c >= 'a' && c <= 'z') {
54-
lettersExisting[c - 'a'] = true;
55-
}
56-
}
57-
for (boolean letterFlag : lettersExisting) {
58-
if (!letterFlag) {
59-
return false;
60-
}
61-
}
62-
return true;
15+
@Test
16+
public void testIsPangramUsingSet() {
17+
assertTrue(Pangram.isPangramUsingSet("The quick brown fox jumps over the lazy dog"));
18+
assertFalse(Pangram.isPangramUsingSet("The quick brown fox jumps over the azy dog")); // L is missing
19+
assertFalse(Pangram.isPangramUsingSet("+-1234 This string is not alphabetical"));
20+
assertFalse(Pangram.isPangramUsingSet("\u0000/\\"));
6321
}
6422

65-
/**
66-
* Checks if a String is a Pangram by checking each letter in the alphabet individually.
67-
*
68-
* @param s The String to check
69-
* @return {@code true} if s is a Pangram, otherwise {@code false}
70-
*/
71-
public static boolean isPangram2(String s) {
72-
if (s.length() < 26) {
73-
return false;
74-
}
75-
s = s.toLowerCase();
76-
for (char i = 'a'; i <= 'z'; i++) {
77-
if (s.indexOf(i) == -1) {
78-
return false; // if any alphabet is missing, return false
79-
}
80-
}
81-
return true;
23+
@Test
24+
public void testIsPangram2() {
25+
assertTrue(Pangram.isPangram2("The quick brown fox jumps over the lazy dog"));
26+
assertFalse(Pangram.isPangram2("The quick brown fox jumps over the azy dog")); // L is missing
27+
assertFalse(Pangram.isPangram2("+-1234 This string is not alphabetical"));
28+
assertFalse(Pangram.isPangram2("\u0000/\\"));
8229
}
8330

84-
/**
85-
* Optimized Pangram check using Bitwise operations.
86-
* Each bit in a 32-bit integer represents a unique letter from 'a' to 'z'.
87-
*
88-
* @param s The String to check
89-
* @return {@code true} if s is a Pangram, otherwise {@code false}
90-
*/
91-
public static boolean isPangramUsingBitwise(String s) {
92-
int checker = 0;
93-
s = s.toLowerCase();
94-
for (char c : s.toCharArray()) {
95-
if (c >= 'a' && c <= 'z') {
96-
int bitIndex = c - 'a';
97-
checker |= (1 << bitIndex);
98-
}
99-
}
100-
// If all 26 bits are set, checker will equal 0b11111111111111111111111111 (26 ones)
101-
return checker == (1 << 26) - 1;
31+
@Test
32+
public void testIsPangramUsingBitwise() {
33+
assertTrue(Pangram.isPangramUsingBitwise("The quick brown fox jumps over the lazy dog"));
34+
assertFalse(Pangram.isPangramUsingBitwise("Hello, World!"));
10235
}
10336
}

0 commit comments

Comments
 (0)