HackerRank Pangrams Solution

Hello Programmers, In this post, you will learn how to solve HackerRank Pangrams Solution. This problem is a part of the HackerRank Algorithms Series.

HackerRank Pangrams Solution
HackerRank Pangrams Solution

One more thing to add, don’t directly look for the solutions, first try to solve the problems of Hackerrank by yourself. If you find any difficulty after trying several times, then you can look for solutions.

HackerRank Pangrams Solution

Task

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 the 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 s.

Constraints

  • 0 < length of s <= 103
  • Each character of ss[i] ∈ {a – z, A – Z, space}

Sample Input 0

We promptly judged antique ivory buckles for the next prize

Sample Output 0

pangram

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

Explanation 1

The string lacks an x

HackerRank Pangrams Solution

Pangrams 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 Solution in Cpp

#include <iostream>
#include <cstdio>
#include <string>
using namespace std;
int main(){
    string s;
    getline(cin, s);
    int cnt[26];
    for(int i =0; i < 26; ++i)
        cnt[i] = 0;
    for(int i = 0; i < s.length(); ++i){
        if(s[i] >= 'a' && s[i] <= 'z'){
            cnt[s[i] - 'a']++;
        }
        if(s[i] >= 'A' && s[i] <= 'Z'){
            cnt[s[i] - 'A']++;
        }
    }
    bool f = true;
    for(int i = 0; i < 26; ++i){
        if(cnt[i] == 0){
            f = false;
            break;
        }
    }
    if(f){
        cout<<"pangram";
    }
    else{
        cout<<"not pangram";
    }
    return 0;
}

Pangrams 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();
    }
}
Ezoicreport this ad

Pangrams 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 Solution using JavaScript

process.stdin.resume();
var _input = '';
process.stdin.on('data', function (data) {
   _input += data; 
});
process.stdin.on('end', function () {
   solve(_input); 
});
function solve(input) {
    var letters = {};
    for (var i = 0; i < input.length; ++i) {
        letters[input.charAt(i).toLowerCase()] = true;
    }
    var isPangram = true;
    var alphabet = 'abcdefghijklmnopqrstuvwxyz';
    for (i = 0; i < alphabet.length; ++i) {
        isPangram = isPangram && letters[alphabet.charAt(i)];
    }
    console.log((isPangram ? '' : 'not ') + 'pangram');

Pangrams Solution in Scala

import scala.collection._
object Solution {
  def isPanagram(s: String): Boolean = {
    val alphabet = mutable.HashSet[Char]()
    for (c <- s if c.isLetter) alphabet.add(c.toLower)
    return alphabet.size == 26
  }
  
  def main(args: Array[String]) {
    val s = io.Source.stdin.getLines().take(1).next
    if (isPanagram(s)) println("pangram")
    else println("not pangram")
  }
}

Pangrams Solution in Pascal

{$r-,s-,q-,i-,o+}
var
  s:ansistring;c:char;i:longint;
  f:array['a'..'z']of boolean;
begin
  readln(s);
  for i:=1 to length(s) do if (s[i]<>' ')then f[lowercase(s[i])]:=true;
  for c:='a' to 'z' do if not f[c] then begin writeln('not pangram');halt;end;writeln('pangram');
end.

Disclaimer: This problem (Pangrams) is generated by HackerRank but the Solution is Provided by BrokenProgrammers. This tutorial is only for Educational and Learning purposes.

Next: HackerRank in a String Solution

Sharing Is Caring

Leave a Comment

Ezoicreport this ad