-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1763-longestNiceSubstring.go
65 lines (61 loc) · 1.12 KB
/
1763-longestNiceSubstring.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
56
57
58
59
60
61
62
63
64
65
// https://leetcode-cn.com/problems/longest-nice-substring/
package main
import (
"fmt"
"unicode"
)
/**
* 常规思维
*/
func longestNiceSubstring(s string) (ans string) {
for i := range s {
lower, upper := 0, 0
for j := i; j < len(s); j++ {
if unicode.IsLower(rune(s[j])) {
lower |= 1 << (s[j] - 'a')
} else {
upper |= 1 << (s[j] - 'A')
}
if lower == upper && j-i+1 > len(ans) {
ans = s[i : j+1]
}
}
}
return
}
/**
* 分治法
*/
func longestNiceSubstring2(s string) (ans string) {
if s == "" {
return
}
lower, upper := 0, 0
for _, ch := range s {
if unicode.IsLower(ch) {
lower |= 1 << (ch - 'a')
} else {
upper |= 1 << (ch - 'A')
}
}
if lower == upper {
return s
}
valid := lower & upper
for i := 0; i < len(s); i++ {
start := i
for i < len(s) && valid>>(unicode.ToLower(rune(s[i]))-'a')&1 == 1 {
i++
}
if t := longestNiceSubstring2(s[start:i]); len(t) > len(ans) {
ans = t
}
}
return
}
func main() {
ss := []string{"YazaAay", "Bb", "c", "dDzeE"}
for _, s := range ss {
fmt.Println(s, " ret:", longestNiceSubstring(s), longestNiceSubstring2(s))
}
}