Pangram Program in Java
If a string comprises all alphabet letters from A to Z or from a to z without regard to case, it is referred to as a pangram.
Some examples of pangram strings or pangram sentences are:
Pangram strings include the following phrases:
- The five boxing wizards jump quickly
- The quick brown fox jumps over the lazy dog
- My ex-pub quiz crowd gave joyful thanks.
- Pack my box with five dozen liquor jugs.
- Fix problem quickly with galvanized jets.
Pangram Algorithm
- Build a hash table (boolean vector). To indicate which characters in the string are present.
- Traverse the entire string of characters that has been provided.
- Subtract "A" from the uppercase letter if it is located to obtain the index.
- If the lowercase letter is discovered, take "a" off to discover the index.
- Mark the vector's value as true (character found).
- Return false if any character is not marked (a character not found).
- If not, return true.
Java program for pangram
Each of the following two methods for locating the pangram string:
- With frequency array
- With Traversal
With Frequency Array
- Each letter should be changed to uppercase or lowercase.
- Make an array to record the frequency of each letter of the given text (from a to z).
- Traverse the frequency spectrum. Print "the string isn't a pangram string" if an alphabet is missing from the frequency array; else, print "the phrase is a pangram string".
Let's include the strategy mentioned above in a Java program.
Pangram.java
public class Pangram
{
static int S = 26;
// function to determine whether or not a character is a letter
static boolean ifalp(char ch)
{
if (!Character.ifalp(ch))
// if the character is not a letter, it returns false.
return false;
// if the character is a letter, it returns true.
return true;
}
// function to determine whether the input string contains all capital letters (a to z).
static boolean letter(String str, int l)
{
// it converts to lowercase when a string is provided.
str = str.toLowerCase();
// making a boolean array that contains the letters that are present
boolean[] flag = new boolean[S];
// loop over the string's characters one by one
for (int i = 0; i < l; i++)
{
// determines whether the current character is a letter.
if (ifalp(str.charAt(i)))
{
int letter = str.charAt(i) - 'a';
// the current letter is deemed to be present.
flag[letter] = true;
}
}
// cycle through each letter in the string provided in a loop.
for (int i = 0; i < S; i++)
{
if (!flag[i])
// if the present letter is not found in the string, it returns false.
return false;
}
// returns true if the string contains the current character.
return true;
}
public static void main(String args[])
{
String str = " The five boxing wizards jump quickly";
int l = str.length();
if (letter(str, l))
System.out.println("The string is a pangram ");
else
System.out.println("The string is not a pangram ");
}
}
Output
The string is a pangram.
With Traversal
In this method, all the letters are first changed to lowercase. Then, go over each character, starting with a and ending with z. Check to see if the given string contains every letter (a to z); if so, print the pangram; otherwise, print the non-pangram.
Let's include the strategy mentioned above in a Java programme.
Pangram1.java
public class Pangram1
{
// function to determine whether the input string contains all capital letters (a to z).
public static void containsall(String str)
{
// it converts to lowercase when a string is provided
str = str.toLowerCase();
boolean all = true;
// loop over the string's characters one by one
for (char ch = 'a'; ch <= 'z'; ch++)
{
// determines whether the current character is a letter.
if (!str.contains(String.valueOf(ch)))
{
all = false;
break;
}
}
// determines whether or not all of the letters are present.
if (all)
System.out.println("the present string is Pangram String");
else
System.out.println("the present string is Not a Pangram String");
}
public static void main(String args[])
{
String string = " The five boxing wizards jump quickly";
containsall(string);
}
}
Output
the present string is Pangram String
Time Complexity: The string's length, n, determine the time complexity of the problem, which is O(n).
Space Complexity: It has an O(1) space complexity since no additional space is required.