Given a string s, check if it is a Pangram or not. A Pangram is a sentence containing every letter in the English Alphabet either in lowercase or Uppercase.
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
[Naive Approach] By Searching for each Character - O(26 * 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 <iostream>
#include <algorithm>
using namespace std;
bool checkPangram(string &s) {
// Check every lowercase letter
for (char ch = 'a'; ch <= 'z'; ch++) {
bool found = false;
// Search for the current letter in the string
for (int i = 0; i < s.length(); i++) {
if (ch == tolower(s[i])) {
found = true;
break;
}
}
// If any letter is missing, it is not a pangram
if (!found)
return false;
}
// All letters are present
return true;
}
int main() {
string s = "The quick brown fox jumps over the lazy dog";
if (checkPangram(s))
cout << "true";
else
cout << "false";
return 0;
}
class GFG {
static boolean checkPangram(String s) {
// Check every lowercase letter
for (char ch = 'a'; ch <= 'z'; ch++) {
boolean found = false;
// Search for the current letter in the string
for (int i = 0; i < s.length(); i++) {
if (ch == Character.toLowerCase(s.charAt(i))) {
found = true;
break;
}
}
// If any letter is missing, it is not a pangram
if (!found)
return false;
}
// All letters are present
return true;
}
public static void main(String[] args) {
String s = "The quick brown fox jumps over the lazy dog";
if (checkPangram(s))
System.out.println("true");
else
System.out.println("false");
}
}
from string import ascii_lowercase
def checkPangram(s):
# Check every lowercase letter
for ch in ascii_lowercase:
found = False
# Search for the current letter in the string
for i in range(len(s)):
if ch == s[i].lower():
found = True
break
# If any letter is missing, it is not a pangram
if not found:
return False
# All letters are present
return True
if __name__ == "__main__":
s = "The quick brown fox jumps over the lazy dog"
if checkPangram(s):
print("true")
else:
print("false")
using System;
class GFG {
static bool checkPangram(string s) {
// Check every lowercase letter
for (char ch = 'a'; ch <= 'z'; ch++) {
bool found = false;
// Search for the current letter in the string
for (int i = 0; i < s.Length; i++) {
if (ch == Char.ToLower(s[i])) {
found = true;
break;
}
}
// If any letter is missing, it is not a pangram
if (!found)
return false;
}
// All letters are present
return true;
}
static void Main(string[] args) {
string s = "The quick brown fox jumps over the lazy dog";
if (CheckPangram(s))
Console.WriteLine("true");
else
Console.WriteLine("false");
}
}
function checkPangram(s) {
// Check every lowercase letter
for (let ch = 'a'; ch <= 'z';
ch = String.fromCharCode(ch.charCodeAt(0) + 1)) {
let found = false;
// Search for the current letter in the string
for (let i = 0; i < s.length; i++) {
if (ch === s[i].toLowerCase()) {
found = true;
break;
}
}
// If any letter is missing, it is not a pangram
if (!found)
return false;
}
// All letters are present
return true;
}
// Driver Code
let s = "The quick brown fox jumps over the lazy dog";
if (checkPangram(s))
console.log("true");
else
console.log("false");
Output
true
[Expected Approach] Using Visited Array - O(n) Time and O(26) Space
The idea is to create a visited array of size 26 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 <iostream>
#include <vector>
using namespace std;
bool checkPangram(string &s) {
// Mark the letters present in the string
vector<bool> vis(26, false);
for (int i = 0; i < s.length(); i++) {
// Mark uppercase letters
if ('A' <= s[i] && s[i] <= 'Z')
vis[s[i] - 'A'] = true;
// Mark lowercase letters
else if ('a' <= s[i] && s[i] <= 'z')
vis[s[i] - 'a'] = true;
}
// Check if all letters are present
for (int i = 0; i < 26; i++) {
if (!vis[i])
return false;
}
return true;
}
int main() {
string s = "The quick brown fox jumps over the lazy dog";
if (checkPangram(s))
cout << "true";
else
cout << "false";
return 0;
}
import java.util.*;
class GFG {
public static boolean checkPangram(String s) {
// Mark the letters present in the string
boolean[] vis = new boolean[26];
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
// Mark uppercase letters
if (c >= 'A' && c <= 'Z')
vis[c - 'A'] = true;
// Mark lowercase letters
else if (c >= 'a' && c <= 'z')
vis[c - 'a'] = true;
}
// Check if all letters are present
for (int i = 0; i < 26; i++) {
if (!vis[i])
return false;
}
return true;
}
public static void main(String[] args) {
String s = "The quick brown fox jumps over the lazy dog";
System.out.println(checkPangram(s) ? "true" : "false");
}
}
def checkPangram(s):
# Mark the letters present in the string
vis = [False] * 26
for c in s:
# Mark uppercase letters
if 'A' <= c <= 'Z':
vis[ord(c) - ord('A')] = True
# Mark lowercase letters
elif 'a' <= c <= 'z':
vis[ord(c) - ord('a')] = True
# Check if all letters are present
return all(vis)
if __name__ == "__main__":
s = "The quick brown fox jumps over the lazy dog"
print("true" if checkPangram(s) else "false")
using System;
class GFG {
public static bool checkPangram(string s) {
// Mark the letters present in the string
bool[] vis = new bool[26];
for (int i = 0; i < s.Length; i++) {
char c = s[i];
// Mark uppercase letters
if (c >= 'A' && c <= 'Z')
vis[c - 'A'] = true;
// Mark lowercase letters
else if (c >= 'a' && c <= 'z')
vis[c - 'a'] = true;
}
// Check if all letters are present
for (int i = 0; i < 26; i++) {
if (!vis[i])
return false;
}
return true;
}
static void Main() {
string s = "The quick brown fox jumps over the lazy dog";
Console.WriteLine(checkPangram(s) ? "true" : "false");
}
}
function checkPangram(s) {
// Mark the letters present in the string
let vis = new Array(26).fill(false);
for (let i = 0; i < s.length; i++) {
let c = s[i];
// Mark uppercase letters
if (c >= 'A' && c <= 'Z')
vis[c.charCodeAt(0) - 'A'.charCodeAt(0)] = true;
// Mark lowercase letters
else if (c >= 'a' && c <= 'z')
vis[c.charCodeAt(0) - 'a'.charCodeAt(0)] = true;
}
// Check if all letters are present
for (let i = 0; i < 26; i++) {
if (!vis[i])
return false;
}
return true;
}
// Driver Code
let s = "The quick brown fox jumps over the lazy dog";
console.log(checkPangram(s) ? "true" : "false");
Output
true