Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Pangram.java #6048

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 26 additions & 70 deletions src/main/java/com/thealgorithms/strings/Pangram.java
Original file line number Diff line number Diff line change
@@ -1,80 +1,36 @@
package com.thealgorithms.strings;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.assertFalse;

import java.util.HashSet;
public class PangramTest {

/**
* Wikipedia: https://en.wikipedia.org/wiki/Pangram
*/
public final class Pangram {
private Pangram() {
@Test
public void testIsPangram() {
assertTrue(Pangram.isPangram("The quick brown fox jumps over the lazy dog"));
assertFalse(Pangram.isPangram("The quick brown fox jumps over the azy dog")); // L is missing
assertFalse(Pangram.isPangram("+-1234 This string is not alphabetical"));
assertFalse(Pangram.isPangram("\u0000/\\"));
}

/**
* Test code
*/
public static void main(String[] args) {
assert isPangram("The quick brown fox jumps over the lazy dog");
assert !isPangram("The quick brown fox jumps over the azy dog"); // L is missing
assert !isPangram("+-1234 This string is not alphabetical");
assert !isPangram("\u0000/\\");
@Test
public void testIsPangramUsingSet() {
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/\\"));
}

/**
* 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<>();
s = s.trim().toLowerCase();
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) != ' ') {
alpha.add(s.charAt(i));
}
}
return alpha.size() == 26;
@Test
public void testIsPangram2() {
assertTrue(Pangram.isPangram2("The quick brown fox jumps over the lazy dog"));
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/\\"));
}

/**
* 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}
*/
public static boolean isPangram(String s) {
boolean[] lettersExisting = new boolean[26];
for (char c : s.toCharArray()) {
int letterIndex = c - (Character.isUpperCase(c) ? 'A' : 'a');
if (letterIndex >= 0 && letterIndex < lettersExisting.length) {
lettersExisting[letterIndex] = true;
}
}
for (boolean letterFlag : lettersExisting) {
if (!letterFlag) {
return false;
}
}
return true;
}

/**
* Checks if a String is Pangram or not by checking if each alhpabet is present or not
*
* @param s The String to check
* @return {@code true} if s is a Pangram, otherwise {@code false}
*/
public static boolean isPangram2(String s) {
if (s.length() < 26) {
return false;
}
s = s.toLowerCase(); // Converting s to Lower-Case
for (char i = 'a'; i <= 'z'; i++) {
if (s.indexOf(i) == -1) {
return false; // if any alphabet is not present, return false
}
}
return true;
@Test
public void testIsPangramUsingBitwise() {
assertTrue(Pangram.isPangramUsingBitwise("The quick brown fox jumps over the lazy dog"));
assertFalse(Pangram.isPangramUsingBitwise("Hello, World!"));
}
}
Loading