|
53 | 53 |
|
54 | 54 | <!-- 这里可写通用的实现逻辑 -->
|
55 | 55 |
|
| 56 | +**方法一:双指针** |
| 57 | + |
| 58 | +我们用双指针维护一个区间 $s[j..i]$,使得区间内最多只有一个相邻字符相等。我们用 $cnt$ 记录区间内相邻字符相等的个数,如果 $cnt \gt 1$,那么我们就需要移动左指针 $j$,直到 $cnt \le 1$。每一次,我们更新答案为 $ans = \max(ans, i - j + 1)$。 |
| 59 | + |
| 60 | +时间复杂度 $O(n)$,其中 $n$ 是字符串的长度。空间复杂度 $O(1)$。 |
| 61 | + |
56 | 62 | <!-- tabs:start -->
|
57 | 63 |
|
58 | 64 | ### **Python3**
|
59 | 65 |
|
60 | 66 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
61 | 67 |
|
62 | 68 | ```python
|
63 |
| - |
| 69 | +class Solution: |
| 70 | + def longestSemiRepetitiveSubstring(self, s: str) -> int: |
| 71 | + n = len(s) |
| 72 | + ans = cnt = j = 0 |
| 73 | + for i in range(n): |
| 74 | + if i and s[i] == s[i - 1]: |
| 75 | + cnt += 1 |
| 76 | + while cnt > 1: |
| 77 | + if s[j] == s[j + 1]: |
| 78 | + cnt -= 1 |
| 79 | + j += 1 |
| 80 | + ans = max(ans, i - j + 1) |
| 81 | + return ans |
64 | 82 | ```
|
65 | 83 |
|
66 | 84 | ### **Java**
|
67 | 85 |
|
68 | 86 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
69 | 87 |
|
70 | 88 | ```java
|
71 |
| - |
| 89 | +class Solution { |
| 90 | + public int longestSemiRepetitiveSubstring(String s) { |
| 91 | + int n = s.length(); |
| 92 | + int ans = 0; |
| 93 | + for (int i = 0, j = 0, cnt = 0; i < n; ++i) { |
| 94 | + if (i > 0 && s.charAt(i) == s.charAt(i - 1)) { |
| 95 | + ++cnt; |
| 96 | + } |
| 97 | + while (cnt > 1) { |
| 98 | + if (s.charAt(j) == s.charAt(j + 1)) { |
| 99 | + --cnt; |
| 100 | + } |
| 101 | + ++j; |
| 102 | + } |
| 103 | + ans = Math.max(ans, i - j + 1); |
| 104 | + } |
| 105 | + return ans; |
| 106 | + } |
| 107 | +} |
72 | 108 | ```
|
73 | 109 |
|
74 | 110 | ### **C++**
|
75 | 111 |
|
76 | 112 | ```cpp
|
77 |
| - |
| 113 | +class Solution { |
| 114 | +public: |
| 115 | + int longestSemiRepetitiveSubstring(string s) { |
| 116 | + int n = s.size(); |
| 117 | + int ans = 0; |
| 118 | + for (int i = 0, j = 0, cnt = 0; i < n; ++i) { |
| 119 | + if (i && s[i] == s[i - 1]) { |
| 120 | + ++cnt; |
| 121 | + } |
| 122 | + while (cnt > 1) { |
| 123 | + if (s[j] == s[j + 1]) { |
| 124 | + --cnt; |
| 125 | + } |
| 126 | + ++j; |
| 127 | + } |
| 128 | + ans = max(ans, i - j + 1); |
| 129 | + } |
| 130 | + return ans; |
| 131 | + } |
| 132 | +}; |
78 | 133 | ```
|
79 | 134 |
|
80 | 135 | ### **Go**
|
81 | 136 |
|
82 | 137 | ```go
|
| 138 | +func longestSemiRepetitiveSubstring(s string) (ans int) { |
| 139 | + n := len(s) |
| 140 | + for i, j, cnt := 0, 0, 0; i < n; i++ { |
| 141 | + if i > 0 && s[i] == s[i-1] { |
| 142 | + cnt++ |
| 143 | + } |
| 144 | + for cnt > 1 { |
| 145 | + if s[j] == s[j+1] { |
| 146 | + cnt-- |
| 147 | + } |
| 148 | + j++ |
| 149 | + } |
| 150 | + ans = max(ans, i-j+1) |
| 151 | + } |
| 152 | + return |
| 153 | +} |
| 154 | +
|
| 155 | +func max(a, b int) int { |
| 156 | + if a > b { |
| 157 | + return a |
| 158 | + } |
| 159 | + return b |
| 160 | +} |
| 161 | +``` |
83 | 162 |
|
| 163 | +### **TypeScript** |
| 164 | + |
| 165 | +```ts |
| 166 | +function longestSemiRepetitiveSubstring(s: string): number { |
| 167 | + const n = s.length; |
| 168 | + let ans = 0; |
| 169 | + for (let i = 0, j = 0, cnt = 0; i < n; ++i) { |
| 170 | + if (i > 0 && s[i] === s[i - 1]) { |
| 171 | + ++cnt; |
| 172 | + } |
| 173 | + while (cnt > 1) { |
| 174 | + if (s[j] === s[j + 1]) { |
| 175 | + --cnt; |
| 176 | + } |
| 177 | + ++j; |
| 178 | + } |
| 179 | + ans = Math.max(ans, i - j + 1); |
| 180 | + } |
| 181 | + return ans; |
| 182 | +} |
84 | 183 | ```
|
85 | 184 |
|
86 | 185 | ### **...**
|
|
0 commit comments