Check a String is a Pangram in Java

This example will show you what is a pangram and how to check a string is a pangram in Java program. A string is a pangram if it contains all letters or characters of alphabets (a to z or A to Z).

A pangram or holoalphabetic sentence is a sentence using every letter of a given alphabet at least once. Pangrams have been used to display typefaces, test equipment, and develop skills in handwriting, calligraphy, and keyboarding. The best-known English pangram is “The quick brown fox jumps over the lazy dog”.

A perfect pangram contains every letter of the alphabet only once and can be considered an anagram of the alphabet. The only perfect pangrams of the English alphabet that are known use abbreviations or other non-words, such as “Mr Jock, TV quiz PhD, bags few lynx” or “Quickly Nez Ford, what’s JPG BMX V?”, or use words so obscure that the phrase is hard to understand, such as “Cwm fjord bank glyphs vext quiz”, in which cwm is a loan word from the Welsh language meaning a steep-sided glaciated valley, and vext is an uncommon way to spell vexed.

So a string to be a pangram must have at least 26 unique alphabets. The examples of pangram are given below:

  • “pack my box with five dozen liquor jugs”
  • “Waltz, bad nymph, for quick jigs vex.”
  • “Glib jocks quiz nymph to vex dwarf”
  • “Sphinx of black quartz, judge my vow”
  • “How vexingly quick daft zebras jump!”
  • “The five boxing wizards jump quickly”
  • “Jackdaws love my big sphinx of quartz”
  • “Pack my box with five dozen liquor jugs”
string is a pangram

Prerequisites

Java

Program to check Pangram

Here I am going to write Java programs in various ways to check some strings are pangrams or not.

The following method filters all alphabets or characters distinctly from a given string and if the number of alphabets is 26 then the string should be a pangram.

public static String isPangram(List<String> pangram) {

	final StringBuilder result = new StringBuilder();

	for (String str : pangram) {
		// Set<Integer> chars = new HashSet<>();
		if (str.length() > 25
				&& str.toLowerCase().chars().filter(i -> i >= 'a' && i <= 'z').distinct().count() == 26) {
			// if(str.length() > 25 && str.toLowerCase().chars().filter(i -> i >= 'a' && i
			// <= 'z').filter(chars::add).filter(i -> chars.size() ==
			// 26).findAny().isPresent()) {
			result.append("1");
		} else {
			result.append("0");
		}
	}

	return result.toString();
}

Here in the following method I am checking for all matching alphabets (a to z) in the string.

public static String isPangram2(List<String> pangram) {
	final StringBuilder result = new StringBuilder();

	for (String str : pangram) {
		if (IntStream.range('a', 'z' + 1).allMatch(c -> str.toLowerCase().indexOf(c) >= 0)) {
			result.append("1");
		} else {
			result.append("0");
		}
	}

	return result.toString();
}

In the below method I am adding all unique alphabets and counting total for checking pangram.

public static String isPangram3(List<String> pangram) {
	final StringBuilder result = new StringBuilder();

	for (String str : pangram) {
		int cnt = 0;
		boolean found = false;
		Set<Integer> alphabet = new HashSet<>(26);

		for (char c : str.toLowerCase().toCharArray()) {
			int n = c - 'a';
			if (n >= 0 && n < 26) {
				if (alphabet.add(n)) {
					cnt += 1;
					if (cnt == 26) {
						found = true;
						break;
					}
				}
			}
		}

		if (found) {
			result.append("1");
		} else {
			result.append("0");
		}
	}

	return result.toString();
}

In the following method first replace all except alphabets and then replace duplicates and count the number of remaining alphabets.

public static String isPangram4(List<String> pangram) {
	final StringBuilder result = new StringBuilder();

	for (String str : pangram) {
		str = str.replaceAll("[^a-zA-Z]", "");
		str = str.toLowerCase();
		str = str.replaceAll("(.)(?=.*\\1)", "");
		if (str.length() == 26) {
			result.append("1");
		} else {
			result.append("0");
		}
	}

	return result.toString();
}

The following method simply checks whether each character from a(A) to z(Z) is present or not in the given string.

public static String isPangram5(List<String> pangram) {
	final StringBuilder result = new StringBuilder();

	for (String str : pangram) {
		boolean found = true;
		for (char c = 'a'; c <= 'z'; ++c) {
			if (!str.toLowerCase().contains(String.valueOf(c))) {
				found = false;
				break;
			}
		}

		if (found) {
			result.append("1");
		} else {
			result.append("0");
		}
	}

	return result.toString();
}

The returned result for each of the method would be a string of 1s and 0s based on a list of strings passed to the method.

The whole source code could be downloaded later from the section Source Code.

Testing the Program

The program is tested against the list of example strings which were given earlier. The output from each of the methods will be 101111111.

Source Code

Download

Leave a Reply

Your email address will not be published.