CheckPangram [全字母句]
# 介绍
包含有字母表中所有字母并且言之成义的句子称为全字母句(英语:pangram 或 holoalphabetic sentence,希腊语:pan gramma(意为 “每一个字母”))。全字母句被用于显示字体和测试打字机。英语中最知名的全字母句是 “The quick brown fox jumps over the lazy dog(敏捷的棕色狐狸跳跃过懒惰狗)”。
一般,有趣的全字母句都很短;写出一个包含有最少重复字母的全字母句是一件富有挑战性的工作。长的全字母句在正确的前提下,显著地富有启迪性,或是很幽默,或是很古怪。从某种意义上,全字母句是漏字文(英语:Lipogram)的对立物,因为后者在文章中有意不使用一个或几个特定字母。
# 实现
# JavaScript
检查是否是回文串:
/**
* 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
}
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
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
(?!pattern)
:正向否定预查,在任何不匹配 pattern 的字符串开始处匹配查找字符串。这是一个非获取匹配,也就是说,该匹配不需要获取供以后使用。例如 “Windows (?!95|98|NT|2000)" 能 匹配 “Windows 3.1” 中的 “windows”, 但不能匹配 “Windows 2000” 中的 “Windows”。预查不消耗字符,也就是说,在一个匹配发生后,在最后一次匹配之后立即开始下一次匹配的搜索,而不是从包含预查的字符之后开始。参考:正则表达式大全 - 夏夜・梦无眠 (opens new window)- Backreferences match the same text as previously matched by a capturing group. The backreference \1 (backslash one) references the first capturing group. 参见:Regex Tutorial - Backreferences To Match The Same Text Again (opens new window)
检查是否可以调整为回文串:
/**
* What is a palindrome? https://en.wikipedia.org/wiki/Palindrome
* Receives a string and returns whether it can be rearranged to become a palindrome or not
* The string can only be a palindrome if the count of ALL characters is even or if the ONLY ONE character count is odd
* Input is a string
*
**/
export const palindromeRearranging = (str) => {
// check that input is a string
if (typeof str !== 'string') {
return 'Not a string'
}
// Check if is a empty string
if (str.length === 0) {
return 'Empty string'
}
// First obtain the character count for each character in the string and store it in an object.
// Filter the object's values to only the odd character counts.
const charCounts = [...str].reduce((counts, char) => {
counts[char] = counts[char] ? counts[char] + 1 : 1
return counts
}, {})
// If the length of the resulting array is 0 or 1, the string can be a palindrome.
return Object.values(charCounts).filter(count => count % 2 !== 0).length <= 1
}
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
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
# 参考
编辑 (opens new window)
上次更新: 2022/10/20, 20:03:22