Skip to content

Commit 20ac3ff

Browse files
Update Pangram.java
Updated the Pangram.java file by adding a bitwise approach and improving code readability
1 parent bca8d0e commit 20ac3ff

File tree

1 file changed

+39
-16
lines changed

1 file changed

+39
-16
lines changed

src/main/java/com/thealgorithms/strings/Pangram.java

+39-16
Original file line numberDiff line numberDiff line change
@@ -3,52 +3,55 @@
33
import java.util.HashSet;
44

55
/**
6+
* Checks if a given string is a Pangram (a sentence containing every letter of the alphabet at least once).
67
* Wikipedia: https://en.wikipedia.org/wiki/Pangram
78
*/
89
public final class Pangram {
10+
911
private Pangram() {
1012
}
1113

1214
/**
13-
* Test code
15+
* Main method to test all pangram-checking methods.
1416
*/
1517
public static void main(String[] args) {
1618
assert isPangram("The quick brown fox jumps over the lazy dog");
1719
assert !isPangram("The quick brown fox jumps over the azy dog"); // L is missing
1820
assert !isPangram("+-1234 This string is not alphabetical");
1921
assert !isPangram("\u0000/\\");
22+
assert isPangramUsingBitwise("The quick brown fox jumps over the lazy dog");
23+
assert !isPangramUsingBitwise("Hello, World!");
2024
}
2125

2226
/**
23-
* Checks if a String is considered a Pangram
27+
* Checks if a String is a Pangram using a HashSet to track unique characters.
2428
*
2529
* @param s The String to check
2630
* @return {@code true} if s is a Pangram, otherwise {@code false}
2731
*/
28-
// alternative approach using Java Collection Framework
2932
public static boolean isPangramUsingSet(String s) {
30-
HashSet<Character> alpha = new HashSet<>();
31-
s = s.trim().toLowerCase();
32-
for (int i = 0; i < s.length(); i++) {
33-
if (s.charAt(i) != ' ') {
34-
alpha.add(s.charAt(i));
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);
3538
}
3639
}
37-
return alpha.size() == 26;
40+
return uniqueChars.size() == 26;
3841
}
3942

4043
/**
41-
* Checks if a String is considered a Pangram
44+
* Checks if a String is a Pangram by tracking occurrences of each letter in an array.
4245
*
4346
* @param s The String to check
4447
* @return {@code true} if s is a Pangram, otherwise {@code false}
4548
*/
4649
public static boolean isPangram(String s) {
4750
boolean[] lettersExisting = new boolean[26];
51+
s = s.toLowerCase();
4852
for (char c : s.toCharArray()) {
49-
int letterIndex = c - (Character.isUpperCase(c) ? 'A' : 'a');
50-
if (letterIndex >= 0 && letterIndex < lettersExisting.length) {
51-
lettersExisting[letterIndex] = true;
53+
if (c >= 'a' && c <= 'z') {
54+
lettersExisting[c - 'a'] = true;
5255
}
5356
}
5457
for (boolean letterFlag : lettersExisting) {
@@ -60,7 +63,7 @@ public static boolean isPangram(String s) {
6063
}
6164

6265
/**
63-
* Checks if a String is Pangram or not by checking if each alhpabet is present or not
66+
* Checks if a String is a Pangram by checking each letter in the alphabet individually.
6467
*
6568
* @param s The String to check
6669
* @return {@code true} if s is a Pangram, otherwise {@code false}
@@ -69,12 +72,32 @@ public static boolean isPangram2(String s) {
6972
if (s.length() < 26) {
7073
return false;
7174
}
72-
s = s.toLowerCase(); // Converting s to Lower-Case
75+
s = s.toLowerCase();
7376
for (char i = 'a'; i <= 'z'; i++) {
7477
if (s.indexOf(i) == -1) {
75-
return false; // if any alphabet is not present, return false
78+
return false; // if any alphabet is missing, return false
7679
}
7780
}
7881
return true;
7982
}
83+
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;
102+
}
80103
}

0 commit comments

Comments
 (0)