Pangram Check
Problem
A pangram uses every letter of the English alphabet at least once. Ignore case, spaces and other characters. Print true if the line is a pangram, else false.
Constraints
- 1 <= length <= 10^5
Examples
Input
The quick brown fox jumps over the lazy dog
Output
true
Solutions
Count the different letters. If there are 26, every letter appeared. Characters that are not a-z must be ignored.
/*
* Problem : Process text.
* Input : One line of text.
* Output : true or false.
* Approach: Mark each letter.
* Time : O(n)
* Space : O(1)
* Java : 8 or newer.
*/
import java.util.Scanner;
public class Main {
// This method solves the task. It receives the input and returns the answer.
static boolean solve(String s) {
// Step 1: One flag for each letter. seen[0] is for a, seen[1] is for b, and so on.
boolean[] seen = new boolean[26];
int count = 0;
for (int i = 0; i < s.length(); i++) {
char letter = Character.toLowerCase(s.charAt(i));
// Step 2: Count each letter the first time we see it.
if (letter >= 'a' && letter <= 'z' && !seen[letter - 'a']) {
seen[letter - 'a'] = true;
count++;
}
}
// Step 3: A pangram has all 26 letters.
return count == 26;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String s = scanner.hasNextLine() ? scanner.nextLine() : "";
System.out.println(solve(s));
}
}
Hints (1)
- How many different letters must a pangram have?