Add pangram check tests (#3267)

This commit is contained in:
0x3C50
2022-09-15 15:31:11 +02:00
committed by GitHub
parent 8f18b92f6e
commit 9c418ba827
2 changed files with 20 additions and 30 deletions

View File

@ -6,34 +6,31 @@ package com.thealgorithms.strings;
public class Pangram {
/**
* Driver Code
* 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");
/* not exists l character */
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/\\");
}
/**
* Check if a string is a pangram string or not
* Checks if a String is considered a Pangram
*
* @param s string to check
* @return {@code true} if given string is pangram, otherwise {@code false}
* @param s The String to check
* @return {@code true} if s is a Pangram, otherwise {@code false}
*/
public static boolean isPangram(String s) {
boolean[] marked = new boolean[26];
/* by default all letters don't exists */
char[] values = s.toCharArray();
for (char value : values) {
if (Character.isLetter(value)) {
int index = Character.isUpperCase(value) ? value - 'A' : value - 'a';
marked[index] = true;
/* mark current character exists */
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 b : marked) {
if (!b) {
for (boolean letterFlag : lettersExisting) {
if (!letterFlag) {
return false;
}
}