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 <debasishbsws.dev@gmail.com> * 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 <debasishbsws.dev@gmail.com>
This commit is contained in:
parent
e5d33f3565
commit
ee2629c8ab
@ -1,5 +1,7 @@
|
||||
package com.thealgorithms.strings;
|
||||
|
||||
import java.util.HashSet;
|
||||
|
||||
/**
|
||||
* Wikipedia: https://en.wikipedia.org/wiki/Pangram
|
||||
*/
|
||||
@ -15,6 +17,22 @@ public class Pangram {
|
||||
assert !isPangram("\u0000/\\");
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a String is considered a Pangram
|
||||
*
|
||||
* @param s The String to check
|
||||
* @return {@code true} if s is a Pangram, otherwise {@code false}
|
||||
*/
|
||||
// alternative approach using Java Collection Framework
|
||||
public static boolean isPangramUsingSet(String s) {
|
||||
HashSet<Character> alpha = new HashSet<Character>();
|
||||
s = s.trim().toLowerCase();
|
||||
for (int i = 0; i < s.length(); i++)
|
||||
if (s.charAt(i) != ' ') alpha.add(s.charAt(i));
|
||||
if (alpha.size() == 26) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a String is considered a Pangram
|
||||
*
|
||||
|
@ -17,5 +17,10 @@ public class PangramTest {
|
||||
assertFalse(Pangram.isPangram2("The quick brown fox jumps over the azy dog")); // L is missing
|
||||
assertFalse(Pangram.isPangram2("+-1234 This string is not alphabetical"));
|
||||
assertFalse(Pangram.isPangram2("\u0000/\\ Invalid characters are alright too"));
|
||||
|
||||
assertTrue(Pangram.isPangramUsingSet("The quick brown fox jumps over the lazy dog"));
|
||||
assertFalse(Pangram.isPangramUsingSet("The quick brown fox jumps over the azy dog")); // L is missing
|
||||
assertFalse(Pangram.isPangramUsingSet("+-1234 This string is not alphabetical"));
|
||||
assertFalse(Pangram.isPangramUsingSet("\u0000/\\ Invalid characters are alright too"));
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user