Pangrams HackerRank Solution in C, C++, Java, Python

A pangram is a string that contains every letter of the alphabet. Given a sentence determine whether it is a pangram in the English alphabet. Ignore case. Return either pangram or not pangram as appropriate.

Example

s = ‘The quick brown fox jumps over lazy dog’ 

The string contains all letters in the English alphabet, so return pangram.

Function Description

Complete the function pangrams in the editor below. It should return the string pangram if the input string is a pangram. Otherwise, it should return not pangram.

pangrams has the following parameter(s):

  • string s: a string to test

Returns

  • string: either pangram or not pangram

Input Format

A single line with string .

Constraints

0<= length of s <= 10^3

Sample Input

Sample Input 0

We promptly judged antique ivory buckles for the next prize

Sample Output 0

pangram

Sample Explanation 0

All of the letters of the alphabet are present in the string.

Sample Input 1

We promptly judged antique ivory buckles for the prize

Sample Output 1

not pangram

Sample Explanation 0

The string lacks an x.

 

Pangrams HackerRank Solution in C

#include<stdio.h>
char st[100000];
int i,ind[1000];
int main()
{
    while(gets(st))
    {
        for(i='A';i<='Z';i++)
        ind[i]=0;

        for(i=0;st[i];i++)
        {
            if(st[i]>='a' && st[i]<='z')
            st[i]-=32;

            ind[st[i]]++;
        }
        for(i='A';i<='Z';i++)
        if(ind[i]==0)
        break;

        if(i=='Z'+1)
        printf("pangram\n");
        else
        printf("not pangram\n");
    }
    return 0;
}

 

Pangrams HackerRank Solution in C++

/*
*/

//#pragma comment(linker, "/STACK:16777216")
#include <fstream>
#include <iostream>
#include <string>
#include <complex>
#include <math.h>
#include <set>
#include <vector>
#include <map>
#include <queue>
#include <stdio.h>
#include <stack>
#include <algorithm>
#include <list>
#include <ctime>
#include <memory.h>

#define y0 sdkfaslhagaklsldk
#define y1 aasdfasdfasdf
#define yn askfhwqriuperikldjk
#define j1 assdgsdgasghsf
#define tm sdfjahlfasfh
#define lr asgasgash

#define eps 1e-11
//#define M_PI 3.141592653589793
#define bs 1000000007
#define bsize 256
#define right adsgasgadsg
#define rmost agasdgasdg

using namespace std;

string st;
long calc[2000],er;

int main(){
//freopen("paired.in","r",stdin);
//freopen("paired.out","w",stdout);
//freopen("C:/input.txt","r",stdin);
//freopen("C:/output.txt","w",stdout);
ios_base::sync_with_stdio(0);
//cin.tie(0);

getline(cin,st);
for (int i=0;i<st.size();i++)
{
 if (st[i]>='a'&&st[i]<='z')
 st[i]=st[i]-'a'+'A';
 calc[st[i]]=1;
}
er=0;
for (int i='A';i<='Z';i++)
if (calc[i]==0)er=1;
if (er)cout<<"not pangram"<<endl;
else cout<<"pangram"<<endl;

cin.get();cin.get();
return 0;}

 

Pangrams HackerRank Solution in Java

import java.io.*;
import java.math.BigInteger;
import java.util.*;


public class Solution {

    void solve() throws IOException {
        String s=reader.readLine();
        boolean[] a=new boolean[26];
        s=s.toLowerCase();
        for(int i=0;i<s.length();i++)
            if(s.charAt(i)>='a'&&s.charAt(i)<='z')a[s.charAt(i)-'a']=true;
        boolean good=true;
        for(int i=0;i<26;i++)
            if(!a[i]){
                good=false;
                break;
            }
        if(good)
            out.println("pangram");
        else
            out.println("not pangram");
    }

    public static void main(String[] args) throws IOException {
        new Solution().run();
    }

    void run() throws IOException {
        reader = new BufferedReader(new InputStreamReader(System.in));
//		reader = new BufferedReader(new FileReader("input.txt"));
        tokenizer = null;
        out = new PrintWriter(new OutputStreamWriter(System.out));
//		out = new PrintWriter(new FileWriter("output.txt"));
        solve();
        reader.close();
        out.flush();

    }

    BufferedReader reader;
    StringTokenizer tokenizer;
    PrintWriter out;

    int nextInt() throws IOException {
        return Integer.parseInt(nextToken());
    }

    long nextLong() throws IOException {
        return Long.parseLong(nextToken());
    }

    double nextDouble() throws IOException {
        return Double.parseDouble(nextToken());
    }

    String nextToken() throws IOException {
        while (tokenizer == null || !tokenizer.hasMoreTokens()) {
            tokenizer = new StringTokenizer(reader.readLine());
        }
        return tokenizer.nextToken();
    }
}

 

Pangrams HackerRank Solution in Python

from string import ascii_lowercase
x = list(ascii_lowercase)
for y in raw_input():
    y = y.lower()
    if y in x:
        x.pop(x.index(y))
if len(x) == 0:
    print "pangram"
else:
    print "not pangram"

 

Pangrams HackerRank Solution in C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace hackerrank_101_hack_Aug14
{
    class Program
    {
        static void Main(string[] args)
        {
            string sentence = Console.ReadLine();
            int []A = new int[26];
            for (int i = 0; i < 26; ++i)
                A[i] = 0;
            sentence = sentence.ToLower();
            for(int i = 0; i < sentence.Length; ++i)
            {
                if(sentence[i] != ' ')
                {
                    A[sentence[i] - 'a'] = 1;
                }
            }
            int j = 0;
            for (j = 0; j < 26; ++j)
                if (A[j] == 0)
                    break;
            if (j == 26)
                Console.WriteLine("pangram");
            else
                Console.WriteLine("not pangram");
        }
    }
}

 

Attempt Pangrams HackerRank Challenge

Link – https://www.hackerrank.com/challenges/pangrams/

Next HackerRank Challenge Solution 

Link – https://exploringbits.com/weighted-uniform-strings-hackerrank-solution/

 

Leave a Comment