Check whether given string is Panagram String or not

ā€œCheck whether given string is Panagram String or notā€ is a very popular and basic problem of string data structure, asked in many technical interviews. Here, we are given a string of length ā€˜nā€™ and our task is to check whether given string is panagram string or not.

Panagram String: A string is called Panagram String when it contains all the alphabets, irrespective of capitalization.

Example:

INPUT:
Qwertyuiopasdfghjklzxcvbnm
OUTPUT:
The given string is a panagram string.

The steps required to check whether the string is panagram string or not are as follows:

  1. Create an hash array of size 123 and initialize it with 0.( 123 size is taken because the ascii value of A-Z is from 65 to 90 and ascii value of a-z is from 97 to 122).
  2. For every alphabet encountered, increment the hash array value for both small and capital alphabet ascii value index.
  3. Finally, iterate over hash array from 65 to 90, if any value is zero, then string is not panagram, else string is panagram.

The time complexity of this solution is O(n), for scanning the length of string.

C++ Program to check whether string is panagram or not is as follows:

/*C++ Program to check if string is panagram or not*/  
#include<bits/stdc++.h>  
#define size 1000  
using namespace std;  
int main()  
{  
    string str;  
    cout<<"Enter a string:\n";  
    getline(cin,str);  
    int flag1 = 0;  
    /* Create a Hash Array */  
    int hash[123] = {0};  
    /* For every alphabet, increment the  
       hash-array value for that character */  
    for(int i = 0; i <  str.length(); i++)  
    {  
        if(str[i]>='a' && str[i]<='z')  
        {  
            hash[str[i]]++; /* Increment for small alphabet character */  
            hash[str[i]-32]++; /* Increment for capital alphabet character */  
        }  
        else if(str[i]>='A' && str[i]<='Z')  
        {  
            hash[str[i]]++; /* Increment for Capital alphabet character */  
            hash[str[i]+32]++; /* Increment for small alphabet character */  
        }  
    }  
    /* Check if any hash array value from 65 to 90 is 0 or not */  
    for(int i = 65; i <= 90; i++)  
    {  
        /* If any hash-array value is 0, then  
        that string is not panagram*/  
        if(hash[i] == 0 || hash[i+32] == 0)  
        {  
            flag1 = 1;  
            cout<<"\nThe given string is not panagram";  
            break;  
        }  
    }  
    if(flag1 == 0)  
    {  
        cout<<"\nThe given string is panagram";  
    }  
}  
OUTPUT:
Enter a string: qwertyuiopasdfghjklzxcvbnm
The given string is panagram

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *