Skip to content

Commit a7ee1d2

Browse files
authored
Merge branch 'master' into secondMinMax
2 parents ed63297 + ee2629c commit a7ee1d2

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.thealgorithms.strings;
22

3+
import java.util.HashSet;
4+
35
/**
46
* Wikipedia: https://en.wikipedia.org/wiki/Pangram
57
*/
@@ -15,6 +17,22 @@ public static void main(String[] args) {
1517
assert !isPangram("\u0000/\\");
1618
}
1719

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+
1836
/**
1937
* Checks if a String is considered a Pangram
2038
*

src/test/java/com/thealgorithms/strings/PangramTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,10 @@ public void testPangram() {
1717
assertFalse(Pangram.isPangram2("The quick brown fox jumps over the azy dog")); // L is missing
1818
assertFalse(Pangram.isPangram2("+-1234 This string is not alphabetical"));
1919
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"));
2025
}
2126
}

0 commit comments

Comments
 (0)