-
Notifications
You must be signed in to change notification settings - Fork 1
/
wrap.go
55 lines (52 loc) · 1021 Bytes
/
wrap.go
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
package wrap
import (
"github.com/catorpilor/leetcode/utils"
)
func NumOfSubStrings(p string) int {
n := len(p)
if n <= 1 {
return n
}
// dp[i] represents the max unique substring ending with ith
// 0 - 'a', ....25 - 'z'
dp := make([]int, 26)
maxLen := 0
for i := range p {
if i > 0 && (p[i]-p[i-1] == 1 || p[i-1]-p[i] == 25) {
maxLen += 1
} else {
maxLen = 1
}
idx := p[i] - 'a'
// just store the longest substring
// for example abcdbcd
// the longest substring ending with 'd' is 4 'abcd' which overlap the 2nd 'bcd'
dp[idx] = utils.Max(dp[idx], maxLen)
}
var ret int
for i := range dp {
ret += dp[i]
}
return ret
}
func NumOfSubStrings2(p string) int {
// brute force
n := len(p)
if n <= 1 {
return n
}
hash := make(map[string]bool)
for i := 0; i < n; i++ {
for j := i; j < n; j++ {
if j > i && (p[j]-p[j-1] != 1 && p[j-1]-p[j] != 25) {
break
}
if j+1 < n {
hash[p[i:j+1]] = true
} else {
hash[p[i:]] = true
}
}
}
return len(hash)
}