-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2981.go
144 lines (125 loc) · 3.19 KB
/
2981.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Source: https://leetcode.com/problems/find-longest-special-substring-that-occurs-thrice-i
// Title: Find Longest Special Substring That Occurs Thrice I
// Difficulty: Medium
// Author: Mu Yang <http://muyang.pro>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// You are given a string `s` that consists of lowercase English letters.
//
// A string is called **special** if it is made up of only a single character. For example, the string `"abc"` is not special, whereas the strings `"ddd"`, `"zz"`, and `"f"` are special.
//
// Return the length of the **longest special substring** of `s` which occurs **at least thrice** , or `-1` if no special substring occurs at least thrice.
//
// A **substring** is a contiguous **non-empty** sequence of characters within a string.
//
// **Example 1:**
//
// ```
// Input: s = "aaaa"
// Output: 2
// Explanation: The longest special substring which occurs thrice is "aa": substrings "**aa** aa", "a**aa** a", and "aa**aa** ".
// It can be shown that the maximum length achievable is 2.
// ```
//
// **Example 2:**
//
// ```
// Input: s = "abcdef"
// Output: -1
// Explanation: There exists no special substring which occurs at least thrice. Hence return -1.
// ```
//
// **Example 3:**
//
// ```
// Input: s = "abcaba"
// Output: 1
// Explanation: The longest special substring which occurs thrice is "a": substrings "**a** bcaba", "abc**a** ba", and "abcab**a** ".
// It can be shown that the maximum length achievable is 1.
// ```
//
// **Constraints:**
//
// - `3 <= s.length <= 50`
// - `s` consists of only lowercase English letters.
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
package main
func maximumLength(s string) int {
n := len(s)
res := -1
// Loop for substring length
LoopLength:
for l := 1; l <= n; l++ {
count := [26]int{}
// Loop for substrings
LoopSubstr:
for i := 0; i <= n-l; i++ {
// Check if is special
ch := s[i]
for j := 1; j < l; j++ {
if s[i+j] != ch {
continue LoopSubstr
}
}
idx := ch - 'a'
count[idx]++
if count[idx] == 3 {
res = l
continue LoopLength
}
}
}
return res
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// fn should be be arr[mid] < target for bisect left
// fn should be be arr[mid] <= target for bisect right
func bisectFunc(fn func(int) bool, lo, hi int) int {
for lo < hi {
mid := (lo + hi) / 2
if fn(mid) {
lo = mid + 1
} else {
hi = mid
}
}
return lo
}
// Binary search
func maximumLength2(s string) int {
n := len(s)
lo := 1
hi := n
fn := func(l int) bool {
count := [26]int{}
// Loop for substrings
Loop:
for i := 0; i <= n-l; i++ {
// Check if is special
ch := s[i]
for j := 1; j < l; j++ {
if s[i+j] != ch {
continue Loop
}
}
idx := ch - 'a'
count[idx]++
if count[idx] == 3 {
return true
}
}
return false
}
// Binary search
res := -1
for lo < hi {
mid := (lo + hi) / 2
if fn(mid) {
res = mid
lo = mid + 1
} else {
hi = mid
}
}
return res
}