Showing posts with label Checking Pangram or Not. Show all posts
Showing posts with label Checking Pangram or Not. Show all posts

Checking Pangram or Not

Given string is a Pangram or not? Pangram is a sentence or line of string containing all the alphabet in English at-least once.

Checking Pangram or Not


Examples:
Input: The quick brown fox jumps over the lazy dog
Output: Given string is a Pangram

Input: Hello Java Discover
Output: Given string is NOT a Pangram

Input: Pack my box with five dozen liquor jugs
Output: Given string is a Pangram

Lets see simple program to check whether given string is Pangram or not with O(N) complexity.

  • Here create an integer array of size 26 and count variable to check all 26 characters occurred at-least once. 
  • Iterate through the each characters of given string and convert it to upper case irrespective of alphabets. 
  • Next check for whether its decimal values is between 65 to 90 and if its, then mark as 1 in integer array @ respective index's and also increment the character count.  



public class Pangram {

 public static void main(String[] args) {
  
  int val[] = new int[26];
  int chCount = 0;
  
  String str = "Pack my box with five dozen liquor jugs";
  
  for (int i =0;i<str.length();i++) {
   int tmp = (int)Character.toUpperCase(str.charAt(i));
   if(tmp >= 65 && tmp <=90) {
    if(val[tmp-65] == 0) {
     val[tmp-65] = 1;
     chCount++;
    }
   }
   if(chCount == 26) break;
  }
  
  if(chCount == 26) 
   System.out.println("Given string is a Pangram");
  else  
   System.out.println("Given string is NOT a Pangram");
 }
}


OUTPUT:


Given string is a Pangram