Pangrams

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:

Input:  s="We promptly judged antique ivory buckles for the next prize"
Output: pangram

Approach

Java


public class Pangrams {
    public static void main(String[] args) {
        String s = "We promptly judged antique ivory 
            buckles for the next prize";
        String result = pangrams(s);
        System.out.println(result);
    }

    private static String pangrams(String s) {
        int n = s.length();
        int f[] = new int[26];
        s = s.toLowerCase();
        for (int i = 0; i < n; i++) {
            if (s.charAt(i) != ' ')
            f[s.charAt(i) - 'a']++;
        }
        for (int i = 0; i < 26; i++)
            if (f[i] == 0)
                return "not pangram";
        return "pangram";
    }
}

C++

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

string pangrams(string s)
{
    int n = s.size();
    int f[26] = {0};
    for (int i = 0i < ni++)
    {
        if (s[i] >= 'A' && s[i] <= 'Z')
            s[i] = s[i] + 32;
        f[s[i] - 'a']++;
    }
    for (int i = 0i < 26i++)
        if (f[i] == 0)
            return "not pangram";
    return "pangram";
}

int main()
{

    string s = "We promptly judged antique ivory buckles for the next prize";

    string result = pangrams(s);
    cout << result << "\n";

    return 0;
}


No comments:

Post a Comment