Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Determining a pangram string in JavaScript
A pangram is a string that contains every letter of the English alphabet at least once. Examples include "The quick brown fox jumps over the lazy dog" and "Pack my box with five dozen liquor jugs".
We need to write a JavaScript function that determines whether a given string is a pangram. For this problem, we'll focus on the 26 letters of the English alphabet, ignoring case sensitivity.
How It Works
The algorithm creates an array of all 26 letters, then iterates through the input string. For each letter found in the string, it removes that letter from the alphabet array. If all letters are found, the array becomes empty, indicating a pangram.
Example
const str = 'We promptly judged antique ivory buckles for the next prize';
const isPangram = (str = '') => {
str = str.toLowerCase();
const { length } = str;
const alphabets = 'abcdefghijklmnopqrstuvwxyz';
const alphaArr = alphabets.split('');
for(let i = 0; i
true
false
true
Using Set for Better Performance
A more efficient approach uses a Set to track found letters:
const isPangramSet = (str = '') => {
const letters = new Set();
str = str.toLowerCase();
for(let char of str) {
if(char >= 'a' && char
true
false
Comparison
| Method | Time Complexity | Space Complexity | Readability |
|---|---|---|---|
| Array Splicing | O(n²) | O(1) | Good |
| Set Approach | O(n) | O(1) | Excellent |
Key Points
- Convert input to lowercase for case-insensitive checking
- A pangram must contain all 26 letters of the alphabet
- The Set approach is more efficient for longer strings
- Both methods ignore non-alphabetic characters
Conclusion
Both approaches effectively detect pangrams, but the Set method offers better performance. Use the array method for learning purposes and the Set approach for production code with large inputs.
