File tree Expand file tree Collapse file tree 2 files changed +23
-0
lines changed
main/java/com/thealgorithms/strings
test/java/com/thealgorithms/strings Expand file tree Collapse file tree 2 files changed +23
-0
lines changed Original file line number Diff line number Diff line change 1
1
package com .thealgorithms .strings ;
2
2
3
+ import java .util .HashSet ;
4
+
3
5
/**
4
6
* Wikipedia: https://en.wikipedia.org/wiki/Pangram
5
7
*/
@@ -15,6 +17,22 @@ public static void main(String[] args) {
15
17
assert !isPangram ("\u0000 /\\ " );
16
18
}
17
19
20
+ /**
21
+ * Checks if a String is considered a Pangram
22
+ *
23
+ * @param s The String to check
24
+ * @return {@code true} if s is a Pangram, otherwise {@code false}
25
+ */
26
+ // alternative approach using Java Collection Framework
27
+ public static boolean isPangramUsingSet (String s ) {
28
+ HashSet <Character > alpha = new HashSet <Character >();
29
+ s = s .trim ().toLowerCase ();
30
+ for (int i = 0 ; i < s .length (); i ++)
31
+ if (s .charAt (i ) != ' ' ) alpha .add (s .charAt (i ));
32
+ if (alpha .size () == 26 ) return true ;
33
+ return false ;
34
+ }
35
+
18
36
/**
19
37
* Checks if a String is considered a Pangram
20
38
*
Original file line number Diff line number Diff line change @@ -17,5 +17,10 @@ public void testPangram() {
17
17
assertFalse (Pangram .isPangram2 ("The quick brown fox jumps over the azy dog" )); // L is missing
18
18
assertFalse (Pangram .isPangram2 ("+-1234 This string is not alphabetical" ));
19
19
assertFalse (Pangram .isPangram2 ("\u0000 /\\ Invalid characters are alright too" ));
20
+
21
+ assertTrue (Pangram .isPangramUsingSet ("The quick brown fox jumps over the lazy dog" ));
22
+ assertFalse (Pangram .isPangramUsingSet ("The quick brown fox jumps over the azy dog" )); // L is missing
23
+ assertFalse (Pangram .isPangramUsingSet ("+-1234 This string is not alphabetical" ));
24
+ assertFalse (Pangram .isPangramUsingSet ("\u0000 /\\ Invalid characters are alright too" ));
20
25
}
21
26
}
You can’t perform that action at this time.
0 commit comments