-
Notifications
You must be signed in to change notification settings - Fork 0
/
CheckPangram.js
54 lines (47 loc) · 2.02 KB
/
CheckPangram.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/**
* What is Pangram?
* Pangram is a sentence that contains all the letters in the alphabet https://en.wikipedia.org/wiki/Pangram
*/
/**
* @function checkPangramRegex
* @description - This function check pangram with the help of regex pattern
* @param {string} string
* @returns {boolean}
* @example - checkPangramRegex("'The quick brown fox jumps over the lazy dog' is a pangram") => true
* @example - checkPangramRegex('"Waltz, bad nymph, for quick jigs vex." is a pangram') => true
*/
const checkPangramRegex = (string) => {
if (typeof string !== 'string') {
throw new TypeError('The given value is not a string')
}
/**
* Match all 26 alphabets using regex, with the help of:
* Capturing group - () -> Groups multiple tokens together and creates a capture group for extracting a substring or using a backreference.
* Character set - [a-z] -> Matches a char in the range a to z in case-insensitive for the 'i' flag
* Negative lookahead - (?!) -> Specifies a group that can not match after the main expression (if it matches, the result is discarded).
* Dot - . -> Matches any character except linebreaks. Equivalent to
* Star - * -> Matches 0 or more of the preceding token.
* Numeric reference - \{$n} -> Matches the results of a capture group. E.g. - \1 matches the results of the first capture group & \3 matches the third.
*/
return string.match(/([a-z])(?!.*\1)/gi).length === 26
}
/**
* @function checkPangramSet
* @description - This function detect the pangram sentence by HashSet
* @param {string} string
* @returns {boolean}
*/
const checkPangramSet = (string) => {
if (typeof string !== 'string') {
throw new TypeError('The given value is not a string')
}
const lettersSet = new Set()
for (const letter of string.toUpperCase()) {
if (/[A-Z]/.test(letter)) {
// if the letter is a valid uppercase alphabet then the add method insert the letter to the HashSet
lettersSet.add(letter)
}
}
return lettersSet.size === 26
}
export { checkPangramRegex, checkPangramSet }