|
55 | 55 |
|
56 | 56 | <!-- 这里可写通用的实现逻辑 -->
|
57 | 57 |
|
| 58 | +**方法一:双指针** |
| 59 | + |
| 60 | +我们先统计每个字符出现的次数,记录在数组 `cnt` 中。 |
| 61 | + |
| 62 | +然后我们使用双指针 $i$ 和 $j$,初始时 $i = j = 0$,然后我们不断地向右移动 $j$,直到 $j$ 指向的字符与 $i$ 指向的字符不同,此时我们得到了一个长度为 $l = j - i$ 的子串,其中所有字符都相同。然后我们跳过第 $j$ 个字符,用指针 $k$ 继续向右移动,直到 $k$ 指向的字符与 $i$ 指向的字符不同,此时我们得到了一个长度为 $r = k - j - 1$ 的子串,其中所有字符都相同。此时我们可以得到的最长子串长度为 $min(l + r + 1, cnt[text[i]])$,其中 $cnt[text[i]]$ 表示字符 $text[i]$ 出现的次数。我们将这个值与当前的最大值进行比较,取较大值作为答案。 |
| 63 | + |
| 64 | +时间复杂度为 $O(n)$,空间复杂度 $O(C)$。其中 $n$ 为字符串的长度,而 $C$ 为字符集的大小。本题中 $C = 26$。 |
| 65 | + |
58 | 66 | <!-- tabs:start -->
|
59 | 67 |
|
60 | 68 | ### **Python3**
|
61 | 69 |
|
62 | 70 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
63 | 71 |
|
64 | 72 | ```python
|
65 |
| - |
| 73 | +class Solution: |
| 74 | + def maxRepOpt1(self, text: str) -> int: |
| 75 | + cnt = Counter(text) |
| 76 | + n = len(text) |
| 77 | + ans = i = 0 |
| 78 | + while i < n: |
| 79 | + j = i |
| 80 | + while j < n and text[j] == text[i]: |
| 81 | + j += 1 |
| 82 | + l = j - i |
| 83 | + k = j + 1 |
| 84 | + while k < n and text[k] == text[i]: |
| 85 | + k += 1 |
| 86 | + r = k - j - 1 |
| 87 | + ans = max(ans, min(l + r + 1, cnt[text[i]])) |
| 88 | + i = j |
| 89 | + return ans |
66 | 90 | ```
|
67 | 91 |
|
68 | 92 | ### **Java**
|
69 | 93 |
|
70 | 94 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
71 | 95 |
|
72 | 96 | ```java
|
| 97 | +class Solution { |
| 98 | + public int maxRepOpt1(String text) { |
| 99 | + int[] cnt = new int[26]; |
| 100 | + int n = text.length(); |
| 101 | + for (int i = 0; i < n; ++i) { |
| 102 | + ++cnt[text.charAt(i) - 'a']; |
| 103 | + } |
| 104 | + int ans = 0, i = 0; |
| 105 | + while (i < n) { |
| 106 | + int j = i; |
| 107 | + while (j < n && text.charAt(j) == text.charAt(i)) { |
| 108 | + ++j; |
| 109 | + } |
| 110 | + int l = j - i; |
| 111 | + int k = j + 1; |
| 112 | + while (k < n && text.charAt(k) == text.charAt(i)) { |
| 113 | + ++k; |
| 114 | + } |
| 115 | + int r = k - j - 1; |
| 116 | + ans = Math.max(ans, Math.min(l + r + 1, cnt[text.charAt(i) - 'a'])); |
| 117 | + i = j; |
| 118 | + } |
| 119 | + return ans; |
| 120 | + } |
| 121 | +} |
| 122 | +``` |
| 123 | + |
| 124 | +### **C++** |
| 125 | + |
| 126 | +```cpp |
| 127 | +class Solution { |
| 128 | +public: |
| 129 | + int maxRepOpt1(string text) { |
| 130 | + int cnt[26] = {0}; |
| 131 | + for (char& c : text) { |
| 132 | + ++cnt[c - 'a']; |
| 133 | + } |
| 134 | + int n = text.size(); |
| 135 | + int ans = 0, i = 0; |
| 136 | + while (i < n) { |
| 137 | + int j = i; |
| 138 | + while (j < n && text[j] == text[i]) { |
| 139 | + ++j; |
| 140 | + } |
| 141 | + int l = j - i; |
| 142 | + int k = j + 1; |
| 143 | + while (k < n && text[k] == text[i]) { |
| 144 | + ++k; |
| 145 | + } |
| 146 | + int r = k - j - 1; |
| 147 | + ans = max(ans, min(l + r + 1, cnt[text[i] - 'a'])); |
| 148 | + i = j; |
| 149 | + } |
| 150 | + return ans; |
| 151 | + } |
| 152 | +}; |
| 153 | +``` |
73 | 154 |
|
| 155 | +### **Go** |
| 156 | +
|
| 157 | +```go |
| 158 | +func maxRepOpt1(text string) (ans int) { |
| 159 | + cnt := [26]int{} |
| 160 | + for _, c := range text { |
| 161 | + cnt[c-'a']++ |
| 162 | + } |
| 163 | + n := len(text) |
| 164 | + for i, j := 0, 0; i < n; i = j { |
| 165 | + j = i |
| 166 | + for j < n && text[j] == text[i] { |
| 167 | + j++ |
| 168 | + } |
| 169 | + l := j - i |
| 170 | + k := j + 1 |
| 171 | + for k < n && text[k] == text[i] { |
| 172 | + k++ |
| 173 | + } |
| 174 | + r := k - j - 1 |
| 175 | + ans = max(ans, min(l+r+1, cnt[text[i]-'a'])) |
| 176 | + } |
| 177 | + return |
| 178 | +} |
| 179 | +
|
| 180 | +func max(a, b int) int { |
| 181 | + if a > b { |
| 182 | + return a |
| 183 | + } |
| 184 | + return b |
| 185 | +} |
| 186 | +
|
| 187 | +func min(a, b int) int { |
| 188 | + if a < b { |
| 189 | + return a |
| 190 | + } |
| 191 | + return b |
| 192 | +} |
74 | 193 | ```
|
75 | 194 |
|
76 | 195 | ### **...**
|
|
0 commit comments