-
Notifications
You must be signed in to change notification settings - Fork 227
/
1023-camelcase-matching.js
50 lines (44 loc) · 1.08 KB
/
1023-camelcase-matching.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
/**
* @param {string[]} queries
* @param {string} pattern
* @return {boolean[]}
*/
const camelMatch = function(queries, pattern) {
const res = []
queries.forEach(el => {
let tmp = chk(el, pattern)
if(tmp) res.push(true)
else res.push(false)
})
return res
};
function chk(str, p) {
let pIdx = 0
let sIdx = 0
const sLen = str.length
const pLen = p.length
const Acode = ('A').charCodeAt(0)
const Zcode = ('Z').charCodeAt(0)
let pEnd = false
for(let i = 0; i < pLen; i++) {
let target = p.charAt(i)
while(sIdx < sLen && !pEnd) {
if(str.charCodeAt(sIdx) >= Acode && str.charCodeAt(sIdx) <= Zcode && str.charAt(sIdx) !== target) return false
if(str.charAt(sIdx) === target) {
if(i !== pLen - 1) {
sIdx++
} else {
pEnd = true
}
break
} else {
sIdx++
}
}
if(sIdx >= sLen) return false
}
for(let i = sIdx + 1; pEnd && i < sLen; i++) {
if(str.charCodeAt(i) >= Acode && str.charCodeAt(i) <= Zcode) return false
}
return true
}