Pangrams – HackerRank Solution

In this post, we will solve Pangrams HackerRank Solution. This problem (Pangrams) is a part of HackerRank Problem Solving series.

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.

Solution – Pangrams – HackerRank Solution

C++

#include <bits/stdc++.h>
using namespace std;

int main(void)
{
    string s;
    getline(cin, s);
    int len = s.length();

    if(len < 26){
        cout << "not pangram" << endl;
        return 0;
    }

    int arr[26] = {0};

    for(int i = 0; i < len; i++){
        arr[toupper(s[i]) - 65]++;
    }

    for(int i = 0; i < 26; i++){
        if(!arr[i]){
            cout << "not pangram" << endl;
            return 0;
        }
    }
    cout << "pangram" << endl;

    return 0;
}

Python

#!/usr/bin/env python3

import string
alphabet = string.ascii_lowercase

def is_pangram(s):
    return set(alphabet) < set(s.lower())

if __name__ == "__main__":
    if is_pangram(input().strip()):
        print("pangram")
    else:
        print("not pangram")

Java

import java.util.Scanner;
import java.util.HashSet;

public class Solution {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        String str = scan.nextLine().toLowerCase();
        scan.close();
        
        HashSet<Character> set = new HashSet();
        for (int i = 0; i < str.length(); i++) {
            char ch = str.charAt(i);
            if (Character.isLetter(ch)) {
                set.add(ch);
            }
        }

        System.out.println(set.size() == 26 ? "pangram" : "not pangram");
    }
}

Note: This problem (Pangrams) is generated by HackerRank but the solution is provided by CodingBroz. This tutorial is only for Educational and Learning purpose.

Leave a Comment

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