Check if given String is Pangram or not
Given a string s, the task is to check if it is Pangram or not.
A pangram is a sentence containing all letters of the English Alphabet.
Examples:
Input: s = "The quick brown fox jumps over the lazy dog"
Output: true
Explanation: The input string contains all characters from ‘a’ to ‘z’.Input: s = "The quick brown fox jumps over the dog"
Output: false
Explanation: The input string does not contain all characters from ‘a’ to ‘z’, as ‘l’, ‘z’, ‘y’ are missing
Table of Content
Here MAX_CHAR is the no. of unique characters present in the string (here 26).
[Naive Approach] By Searching for each Character - O(MAX_CHAR* n) Time and O(1) Space
The idea is to iterate over all characters from 'a' to 'z' and for each character, check if it is present in the input string or not. If any character is not present in the input string, return false. Otherwise, return true. Also, we need to ignore the case while searching for a character('a' and 'A' are considered same).
#include <bits/stdc++.h>
using namespace std;
bool checkPangram(string &s) {
for(char ch = 'a'; ch <= 'z'; ch++) {
bool found = false;
for(int i = 0; i < s.length(); i++) {
if(ch == tolower(s[i])) {
found = true;
break;
}
}
if(found == false)
return false;
}
return true;
}
int main() {
string s = "The quick brown fox jumps over the lazy dog";
if (checkPangram(s) == true)
cout << "true";
else
cout << "false";
return 0;
}
class GfG {
static boolean checkPangram(String s) {
for(char ch = 'a'; ch <= 'z'; ch++) {
boolean found = false;
for(int i = 0; i < s.length(); i++) {
if(ch == Character.toLowerCase(s.charAt(i))) {
found = true;
break;
}
}
if(found == false)
return false;
}
return true;
}
public static void main(String[] args) {
String s = "The quick brown fox jumps over the lazy dog";
if (checkPangram(s) == true)
System.out.println("true");
else
System.out.println("false");
}
}
from string import ascii_lowercase
def checkPangram(s):
for ch in ascii_lowercase:
found = False
for i in range(len(s)):
if ch == (s[i].lower()):
found = True
break
if found == False:
return False
return True
if __name__ == "__main__":
s = "The quick brown fox jumps over the lazy dog"
if checkPangram(s) == True:
print("true")
else:
print("false")
using System;
class GfG {
static bool checkPangram(string s) {
for(char ch = 'a'; ch <= 'z'; ch++) {
bool found = false;
for(int i = 0; i < s.Length; i++) {
if(ch == Char.ToLower(s[i])) {
found = true;
break;
}
}
if(found == false)
return false;
}
return true;
}
static void Main(string[] args) {
string s = "The quick brown fox jumps over the lazy dog";
if (checkPangram(s) == true)
Console.WriteLine("true");
else
Console.WriteLine("false");
}
}
function checkPangram(s) {
for (let ch = 'a'; ch <= 'z'; ch = String.fromCharCode(ch.charCodeAt(0) + 1)) {
let found = false;
for (let i = 0; i < s.length; i++) {
if (ch === s[i].toLowerCase()) {
found = true;
break;
}
}
if (found === false)
return false;
}
return true;
}
let s = "The quick brown fox jumps over the lazy dog";
if (checkPangram(s) === true)
console.log("true");
else
console.log("false");
Output
true
[Expected Approach] Using Visited Array - O(n) Time and O(MAX_CHAR) Space
The idea is to create a visited array of size MAX_CHAR to mark whether a character is present in the string or not. Now, iterate through all the characters of the string and mark them as visited. Lowercase and Uppercase are considered the same. So ‘A’ and ‘a’ are marked at index 0 and similarly ‘Z’ and ‘z’ are marked at index 25.
After iterating through all the characters, check whether all the characters are marked in visited array or not. If not then return false else return true.
#include <bits/stdc++.h>
using namespace std;
const int MAX_CHAR = 26;
bool checkPangram(string &s) {
vector<bool> vis(MAX_CHAR, false);
for (int i = 0; i < s.length(); i++) {
// for uppercases
if ('A' <= s[i] && s[i] <= 'Z')
vis[s[i] - 'A'] = true;
// for lowercases
else if ('a' <= s[i] && s[i] <= 'z')
vis[s[i] - 'a'] = true;
}
for (int i = 0; i < MAX_CHAR; i++) {
if (vis[i] == false)
return false;
}
return true;
}
int main() {
string s = "The quick brown fox jumps over the dog";
if (checkPangram(s) == true)
cout << "true";
else
cout << "false";
return 0;
}
import java.util.*;
public class GFG {
static final int MAX_CHAR = 26;
public static boolean checkPangram(String s) {
boolean[] vis = new boolean[MAX_CHAR];
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c >= 'A' && c <= 'Z')
vis[c - 'A'] = true;
else if (c >= 'a' && c <= 'z')
vis[c - 'a'] = true;
}
for (int i = 0; i < MAX_CHAR; i++) {
if (!vis[i])
return false;
}
return true;
}
public static void main(String[] args) {
String s = "The quick brown fox jumps over the dog";
System.out.println(checkPangram(s) ? "true" : "false");
}
}
MAX_CHAR = 26
def checkPangram(s):
vis = [False] * MAX_CHAR
for c in s:
if 'A' <= c <= 'Z':
vis[ord(c) - ord('A')] = True
elif 'a' <= c <= 'z':
vis[ord(c) - ord('a')] = True
for flag in vis:
if not flag:
return False
return True
s = "The quick brown fox jumps over the dog"
print("true" if checkPangram(s) else "false")
using System;
using System.Collections.Generic;
class GFG {
const int MAX_CHAR = 26;
public static bool checkPangram(string s) {
bool[] vis = new bool[MAX_CHAR];
for (int i = 0; i < s.Length; i++) {
char c = s[i];
if (c >= 'A' && c <= 'Z')
vis[c - 'A'] = true;
else if (c >= 'a' && c <= 'z')
vis[c - 'a'] = true;
}
for (int i = 0; i < MAX_CHAR; i++) {
if (!vis[i])
return false;
}
return true;
}
static void Main() {
string s = "The quick brown fox jumps over the dog";
Console.WriteLine(checkPangram(s) ? "true" : "false");
}
}
const MAX_CHAR = 26;
function checkPangram(s) {
let vis = new Array(MAX_CHAR).fill(false);
for (let i = 0; i < s.length; i++) {
let c = s[i];
if (c >= 'A' && c <= 'Z')
vis[c.charCodeAt(0) - 'A'.charCodeAt(0)] = true;
else if (c >= 'a' && c <= 'z')
vis[c.charCodeAt(0) - 'a'.charCodeAt(0)] = true;
}
for (let i = 0; i < MAX_CHAR; i++) {
if (!vis[i])
return false;
}
return true;
}
let s = "The quick brown fox jumps over the dog";
console.log(checkPangram(s) ? "true" : "false");
Output
true