Javatpoint Logo
Javatpoint Logo

Pangram Program in Java

In this section, we will discuss what is a pangram string. We will also create a Java program to check the given string is a pangram or not.

What is pangram string?

The string is called a pangram if it contains all the alphabets from a to z or A to Z, ignoring the case sensitivity.

Example of Pangram Strings or Sentences

The following string/ sentences are pangram strings:

  • Pack my box with five dozen liquor jugs.
  • The quick brown fox jumps over the lazy dog.
  • My ex pub quiz crowd gave joyful thanks.
  • Cozy sphinx waves quart jug of bad milk.
  • Fix problem quickly with galvanized jets.

Algorithm

  1. Create a hash table (boolean vector). To mark the characters, present in the string.
  2. Traverse over all the characters in the given string.
  3. If the uppercase letter is found, subtract 'A' to find the index.
  4. If the lowercase letter is found, subtract 'a' to find the index.
  5. Mark the value in the vector as true (character found).
  6. Return false if any character is unmarked (a character not found).
  7. Else, return true.

Pangram Java Program

In order to find the pangram string, there are the following two approaches:

  • Using Frequency Array
  • Using Traversal

Using Frequency Array

  1. Convert each letter either in lowercase or in uppercase.
  2. Create an array to mark the frequency of each alphabet (from a to z) of the given string.
  3. Traverse over the frequency array. If there is any alphabet that is not present in the frequency array, print "the string is not a pangram string", else print "the string is a pangram string".

Let's implement the above approach in a Java program.

PangramStringExample1.java

Output:

The given string is a pangram string.

Using Traversal

In this approach, first, convert all the letters into lowercase letters. After that, traverse over each character from a to z itself. Check if the given string contains all the letters (a to z), if present, print string id pangram, else print string is not pangram.

Let's implement the above approach in a Java program.

PangramStringExample2.java

Output:

Pangram String

Let's see another approach.

In this approach, we have created a Boolean type array named mark[]. Iterate over all the alphabets presented in the string and mark each visited alphabet. Suppose, a or A is found in the string, marked it at index 0. Similarly, for other alphabets.

PangramStringExample3.java

Output:

Fix problem quickly with galvanized jets is a pangram string.

Let's see another logic for the same.

PangramStringExample4.java

Output:

Enter the string: pack my box with five dozen liquor jugs
The string is a pangram string.

Complexity

Time Complexity: Its time complexity is O(n), where n is the length of the string.

Space Complexity: Its space complexity is O(1) because it does not require extra space.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA