3
3
import java .util .HashSet ;
4
4
5
5
/**
6
+ * Checks if a given string is a Pangram (a sentence containing every letter of the alphabet at least once).
6
7
* Wikipedia: https://en.wikipedia.org/wiki/Pangram
7
8
*/
8
9
public final class Pangram {
10
+
9
11
private Pangram () {
10
12
}
11
13
12
14
/**
13
- * Test code
15
+ * Main method to test all pangram-checking methods.
14
16
*/
15
17
public static void main (String [] args ) {
16
18
assert isPangram ("The quick brown fox jumps over the lazy dog" );
17
19
assert !isPangram ("The quick brown fox jumps over the azy dog" ); // L is missing
18
20
assert !isPangram ("+-1234 This string is not alphabetical" );
19
21
assert !isPangram ("\u0000 /\\ " );
22
+ assert isPangramUsingBitwise ("The quick brown fox jumps over the lazy dog" );
23
+ assert !isPangramUsingBitwise ("Hello, World!" );
20
24
}
21
25
22
26
/**
23
- * Checks if a String is considered a Pangram
27
+ * Checks if a String is a Pangram using a HashSet to track unique characters.
24
28
*
25
29
* @param s The String to check
26
30
* @return {@code true} if s is a Pangram, otherwise {@code false}
27
31
*/
28
- // alternative approach using Java Collection Framework
29
32
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 );
35
38
}
36
39
}
37
- return alpha .size () == 26 ;
40
+ return uniqueChars .size () == 26 ;
38
41
}
39
42
40
43
/**
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.
42
45
*
43
46
* @param s The String to check
44
47
* @return {@code true} if s is a Pangram, otherwise {@code false}
45
48
*/
46
49
public static boolean isPangram (String s ) {
47
50
boolean [] lettersExisting = new boolean [26 ];
51
+ s = s .toLowerCase ();
48
52
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 ;
52
55
}
53
56
}
54
57
for (boolean letterFlag : lettersExisting ) {
@@ -60,7 +63,7 @@ public static boolean isPangram(String s) {
60
63
}
61
64
62
65
/**
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.
64
67
*
65
68
* @param s The String to check
66
69
* @return {@code true} if s is a Pangram, otherwise {@code false}
@@ -69,12 +72,32 @@ public static boolean isPangram2(String s) {
69
72
if (s .length () < 26 ) {
70
73
return false ;
71
74
}
72
- s = s .toLowerCase (); // Converting s to Lower-Case
75
+ s = s .toLowerCase ();
73
76
for (char i = 'a' ; i <= 'z' ; i ++) {
74
77
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
76
79
}
77
80
}
78
81
return true ;
79
82
}
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
+ }
80
103
}
0 commit comments