Skip to content

Commit ee2629c

Browse files
Update Pangram.java using Java Collections (#4479)
* Update Pangram.java using Java Collections A simple separate function isPangramOrNot(String s) has been created which implements the Pangram checking of a string using Java Collection Framework approach. * Update Pangram.java using Java Collections * Updated some linting errors in Pangram.java * Update src/main/java/com/thealgorithms/strings/Pangram.java Co-authored-by: Debasish Biswas <[email protected]> * Updated Pangram.java Method name updated to - isPangramUsingSet(String s) * Updated the testcases PangramTest.java Successfully updated the testcases in this same PR branch --------- Co-authored-by: Debasish Biswas <[email protected]>
1 parent e5d33f3 commit ee2629c

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)