-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspecial-string-again.ts
More file actions
55 lines (46 loc) · 1.36 KB
/
Copy pathspecial-string-again.ts
File metadata and controls
55 lines (46 loc) · 1.36 KB
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
55
/**
*
* A string is said to be a special string if either of two conditions is met:
* All of the characters are the same, e.g. aaa.
* All characters except the middle one are the same, e.g. aadaa.
*
* A special substring is any substring of a string which meets one of those criteria.
* Given a string, determine how many special substrings can be formed from it.
*
* Example:
*
* s = mnonopoo // => contains 12 special substrings
*
* {m, n, o, n, o, p, o, o, non, ono, opo, oo}
*
*/
export function substrCount(n: number, s: string): number {
let total = 0
const groups: Array<{ char: string; length: number }> = []
// Group the same characters
let index = 0
while (index < n) {
const currentChar = s[index]
let length = 1
while (index + 1 < n && s[index + 1] === currentChar) {
length++
index++
}
groups.push({ char: currentChar, length })
index++
}
// Count substrings from identical character groups
for (const group of groups) {
total += (group.length * (group.length + 1)) / 2
}
// Count symmetric patterns (E.g aba, aabaa)
for (let k = 1; k < groups.length - 1; k++) {
const left = groups[k - 1]
const center = groups[k]
const right = groups[k + 1]
if (center.length === 1 && left.char === right.char) {
total += Math.min(left.length, right.length)
}
}
return total
}