-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspecialString.js
More file actions
30 lines (28 loc) · 781 Bytes
/
specialString.js
File metadata and controls
30 lines (28 loc) · 781 Bytes
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
function substrCount(n, s) {
let count = n;
for (let i = 0; i < s.length; i++) {
let nextIndex = i;
while (s[i] === s[nextIndex + 1]) nextIndex++;
if (i !== nextIndex) {
const length = nextIndex - i;
count = count + (length * (length + 1)) / 2;
i = nextIndex;
} else {
let step = 1;
console.log('i + step', s[i+ step])
console.log('i+1', s[i+1])
while (s[i + step] === s[i - step] && s[i + step] === s[i + 1]) {
step++;
count++;
}
}
}
console.log(count)
return count;
}
let str = 'asasd'
let str2 = 'abcbaba'
let str3 = 'aaaa'
substrCount(5, str)
substrCount(7, str2)
// substrCount(4, str3)