HackerRank Pangrams Problem Solution
In this post, we will solve HackerRank Pangrams Problem Solution.
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 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
ornot pangram
Input Format
A single line with string s.
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 C Solution
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define S_MAX 1000
#define N_ALPHABET 26
int main(void) {
int i, j, k, nbr_letters;
int c;
char s[S_MAX+1];
char alpha [][N_ALPHABET+1] = {
"abcdefghijklmnopqrstuvwxyz",
"ABCDEFGHIJKLMNOPQRSTUVWXYZ",
""
};
int n[N_ALPHABET] = {0};
nbr_letters = 0;
fgets(s, S_MAX+1, stdin);
for(i=0; s[i] != '\0'; i++) {
for(j=0; alpha[j][0] != '\0'; j++) {
for (k=0; alpha[j][k] != '\0'; k++) {
if (s[i] == alpha[j][k] && n[k] == 0) {
nbr_letters++;
n[k]++;
}
}
}
}
if (nbr_letters == N_ALPHABET)
puts("pangram");
else
puts("not pangram");
return EXIT_SUCCESS;
}
Pangrams C++ Solution
#include <iostream>
#include <cctype>
#include <bitset>
using namespace std;
int main() {
char c;
static bitset<26> b{false};
while (cin >> c) {
c = tolower(c);
if (isalpha(c)) {
b[c] = true;
}
}
if (b.count() != 26) {
cout << "not ";
}
cout << "pangram" << endl;
return 0;
}
Pangrams C Sharp Solution
using System;
using System.Collections.Generic;
using System.IO;
using System.Collections;
class Solution {
static void Main(String[] args) {
string sentence = Console.ReadLine();
sentence = sentence.ToLower().Replace(" ", "");
Hashtable hash = new Hashtable();
for(int i = 0; i < sentence.Length; i++)
{
try{hash.Add(sentence[i], 1);}
catch(Exception e){}
}
if(hash.Count != 26)
{
Console.WriteLine("not pangram");
}
else
{
Console.WriteLine("pangram");
}
}
}
Pangrams Java Solution
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
class Result {
/*
* Complete the 'pangrams' function below.
*
* The function is expected to return a STRING.
* The function accepts STRING s as parameter.
*/
public static String pangrams(String s) {
// Write your code here
if(s.length() < 26){
return "not pangram";
}else{
List<Character> result = new ArrayList<Character>();
for (char ch: s.toLowerCase().toCharArray()) {
if(!result.contains(ch) && ch != ' '){
result.add(ch);
}
}
if(result.size() < 26){
return "not pangram";
}else{
return "pangram";
}
}
}
}
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
String s = bufferedReader.readLine();
String result = Result.pangrams(s);
bufferedWriter.write(result);
bufferedWriter.newLine();
bufferedReader.close();
bufferedWriter.close();
}
}
Pangrams JavaScript Solution
function processData(input) {
//Enter your code here
D = {}
chars = input.toLowerCase().split('');
chars = chars.filter(function(c) {return 'a' <= c && c <= 'z'; });
for (i in chars) {
D[chars[i]] = 1;
}
if (Object.keys(D).length == 26) {
console.log("pangram");
}
else {
console.log("not pangram");
}
}
process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
_input += input;
});
process.stdin.on("end", function () {
processData(_input);
});
Pangrams Python Solution
import string
s = input()
s = s.lower()
isPangram = True
for letter in string.ascii_lowercase:
isPangram = isPangram and letter in s
if isPangram:
print('pangram')
else:
print('not pangram')
Other Solutions