|
6 | 6 | public class Pangram { |
7 | 7 |
|
8 | 8 | /** |
9 | | - * Driver Code |
| 9 | + * Test code |
10 | 10 | */ |
11 | 11 | public static void main(String[] args) { |
12 | 12 | assert isPangram("The quick brown fox jumps over the lazy dog"); |
13 | | - assert !isPangram("The quick brown fox jumps over the azy dog"); |
14 | | - /* not exists l character */ |
| 13 | + assert !isPangram("The quick brown fox jumps over the azy dog"); // L is missing |
| 14 | + assert !isPangram("+-1234 This string is not alphabetical"); |
| 15 | + assert !isPangram("\u0000/\\"); |
15 | 16 | } |
16 | 17 |
|
17 | 18 | /** |
18 | | - * Check if a string is a pangram string or not |
| 19 | + * Checks if a String is considered a Pangram |
19 | 20 | * |
20 | | - * @param s string to check |
21 | | - * @return {@code true} if given string is pangram, otherwise {@code false} |
| 21 | + * @param s The String to check |
| 22 | + * @return {@code true} if s is a Pangram, otherwise {@code false} |
22 | 23 | */ |
23 | 24 | public static boolean isPangram(String s) { |
24 | | - boolean[] marked = new boolean[26]; |
25 | | - /* by default all letters don't exists */ |
26 | | - char[] values = s.toCharArray(); |
27 | | - for (char value : values) { |
28 | | - if (Character.isLetter(value)) { |
29 | | - int index = Character.isUpperCase(value) ? value - 'A' : value - 'a'; |
30 | | - marked[index] = true; |
31 | | - /* mark current character exists */ |
| 25 | + boolean[] lettersExisting = new boolean[26]; |
| 26 | + for (char c : s.toCharArray()) { |
| 27 | + int letterIndex = c - (Character.isUpperCase(c) ? 'A' : 'a'); |
| 28 | + if (letterIndex >= 0 && letterIndex < lettersExisting.length) { |
| 29 | + lettersExisting[letterIndex] = true; |
32 | 30 | } |
33 | 31 | } |
34 | | - |
35 | | - for (boolean b : marked) { |
36 | | - if (!b) { |
| 32 | + for (boolean letterFlag : lettersExisting) { |
| 33 | + if (!letterFlag) { |
37 | 34 | return false; |
38 | 35 | } |
39 | 36 | } |
|
0 commit comments