-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2730-FindTheLongestSemiRepetitiveSubstring.go
More file actions
126 lines (111 loc) · 4.27 KB
/
2730-FindTheLongestSemiRepetitiveSubstring.go
File metadata and controls
126 lines (111 loc) · 4.27 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
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
package main
// 2730. Find the Longest Semi-Repetitive Substring
// You are given a digit string s that consists of digits from 0 to 9.
// A string is called semi-repetitive if there is at most one adjacent pair of the same digit.
// For example, "0010", "002020", "0123", "2002", and "54944" are semi-repetitive while the following are not: "00101022" (adjacent same digit pairs are 00 and 22), and "1101234883" (adjacent same digit pairs are 11 and 88).
// Return the length of the longest semi-repetitive substring of s.
// Example 1:
// Input: s = "52233"
// Output: 4
// Explanation:
// The longest semi-repetitive substring is "5223". Picking the whole string "52233" has two adjacent same digit pairs 22 and 33, but at most one is allowed.
// Example 2:
// Input: s = "5494"
// Output: 4
// Explanation:
// s is a semi-repetitive string.
// Example 3:
// Input: s = "1111111"
// Output: 2
// Explanation:
// The longest semi-repetitive substring is "11". Picking the substring "111" has two adjacent same digit pairs, but at most one is allowed.
// Constraints:
// 1 <= s.length <= 50
// '0' <= s[i] <= '9'
import "fmt"
func longestSemiRepetitiveSubstring(s string) int {
res, n := 0, len(s)
for i := 1; i < n; i++ {
if s[i] == s[i - 1]{
left := i - 2
for left >= 0 && s[left] != s[left + 1]{
left--
}
right := i + 1
for right < n && s[right] != s[right - 1]{
right++
}
res = max(res,right - left - 1)
}
}
if res > 0 {
return res
}
return n
}
func longestSemiRepetitiveSubstring1(s string) int {
n := len(s)
if n == 1 { return 1 }
max := func (x, y int) int { if x > y { return x; }; return y; }
res, left, last := 0, 0, -1 // 用于记录最近一次重复字符的位置
for right := 1; right < n; right++ {
if s[right] == s[right - 1] { // 如果当前字符和前一个字符重复
if last != -1 { // 如果已经有一个重复字符,调整左边界
left = last + 1
}
last = right - 1 // 更新最近一次重复字符的位置
}
res = max(res, right - left + 1) // 更新最长子串的长度
}
return res
}
func longestSemiRepetitiveSubstring2(s string) int {
res, left, same := 1, 0, 0
max := func (x, y int) int { if x > y { return x; }; return y; }
for right := 1; right < len(s); right++ {
if s[right] == s[right-1] {
same++
}
if same > 1 { // same == 2
left++
for s[left] != s[left-1] {
left++
}
same = 1
}
res = max(res, right - left + 1)
}
return res
}
func main() {
// Example 1:
// Input: s = "52233"
// Output: 4
// Explanation:
// The longest semi-repetitive substring is "5223". Picking the whole string "52233" has two adjacent same digit pairs 22 and 33, but at most one is allowed.
fmt.Println(longestSemiRepetitiveSubstring("52233")) // 4
// Example 2:
// Input: s = "5494"
// Output: 4
// Explanation:
// s is a semi-repetitive string.
fmt.Println(longestSemiRepetitiveSubstring("5494")) // 4
// Example 3:
// Input: s = "1111111"
// Output: 2
// Explanation:
// The longest semi-repetitive substring is "11". Picking the substring "111" has two adjacent same digit pairs, but at most one is allowed.
fmt.Println(longestSemiRepetitiveSubstring("1111111")) // 2
fmt.Println(longestSemiRepetitiveSubstring("123456789")) // 9
fmt.Println(longestSemiRepetitiveSubstring("987654321")) // 9
fmt.Println(longestSemiRepetitiveSubstring1("52233")) // 4
fmt.Println(longestSemiRepetitiveSubstring1("5494")) // 4
fmt.Println(longestSemiRepetitiveSubstring1("1111111")) // 2
fmt.Println(longestSemiRepetitiveSubstring1("123456789")) // 9
fmt.Println(longestSemiRepetitiveSubstring1("987654321")) // 9
fmt.Println(longestSemiRepetitiveSubstring2("52233")) // 4
fmt.Println(longestSemiRepetitiveSubstring2("5494")) // 4
fmt.Println(longestSemiRepetitiveSubstring2("1111111")) // 2
fmt.Println(longestSemiRepetitiveSubstring2("123456789")) // 9
fmt.Println(longestSemiRepetitiveSubstring2("987654321")) // 9
}