Skip to content

Commit b6ce503

Browse files
committed
+ problem 1156
1 parent f1374d2 commit b6ce503

File tree

5 files changed

+169
-0
lines changed

5 files changed

+169
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# 1156. Swap For Longest Repeated Character Substring
2+
You are given a string `text`. You can swap two of the characters in the `text`.
3+
4+
Return *the length of the longest substring with repeated characters*.
5+
6+
#### Example 1:
7+
<pre>
8+
<strong>Input:</strong> text = "ababa"
9+
<strong>Output:</strong> 3
10+
<strong>Explanation:</strong> We can swap the first 'b' with the last 'a', or the last 'b' with the first 'a'. Then, the longest repeated character substring is "aaa" with length 3.
11+
</pre>
12+
13+
#### Example 2:
14+
<pre>
15+
<strong>Input:</strong> text = "aaabaaa"
16+
<strong>Output:</strong> 6
17+
<strong>Explanation:</strong> Swap 'b' with the last 'a' (or the first 'a'), and we get longest repeated character substring "aaaaaa" with length 6.
18+
</pre>
19+
20+
#### Example 3:
21+
<pre>
22+
<strong>Input:</strong> text = "aaaaa"
23+
<strong>Output:</strong> 5
24+
<strong>Explanation:</strong> No need to swap, longest repeated character substring is "aaaaa" with length is 5.
25+
</pre>
26+
27+
#### Constraints:
28+
* <code>1 <= text.length <= 2 * 10<sup>4</sup></code>
29+
* `text` consist of lowercase English characters only.
30+
31+
## Solutions (Rust)
32+
33+
### 1. Solution
34+
```Rust
35+
impl Solution {
36+
pub fn max_rep_opt1(text: String) -> i32 {
37+
let text = text.as_bytes();
38+
let mut ranges = vec![vec![]; 26];
39+
let mut ret = 1;
40+
41+
for i in 0..text.len() {
42+
if i == 0 || text[i] != text[i - 1] {
43+
ranges[(text[i] - b'a') as usize].push((i, i));
44+
} else {
45+
ranges[(text[i] - b'a') as usize].last_mut().unwrap().1 = i;
46+
}
47+
}
48+
49+
for i in 0..ranges.len() {
50+
for j in 0..ranges[i].len() {
51+
ret = ret.max(ranges[i][j].1 - ranges[i][j].0 + 1 + (ranges[i].len() > 1) as usize);
52+
53+
if j > 0 && ranges[i][j].0 == ranges[i][j - 1].1 + 2 {
54+
ret = ret
55+
.max(ranges[i][j].1 - ranges[i][j - 1].0 + (ranges[i].len() > 2) as usize);
56+
}
57+
}
58+
}
59+
60+
ret as i32
61+
}
62+
}
63+
```
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# 1156. 单字符重复子串的最大长度
2+
如果字符串中的所有字符都相同,那么这个字符串是单字符重复的字符串。
3+
4+
给你一个字符串 `text`,你只能交换其中两个字符一次或者什么都不做,然后得到一些单字符重复的子串。返回其中最长的子串的长度。
5+
6+
#### 示例 1:
7+
<pre>
8+
<strong>输入:</strong> text = "ababa"
9+
<strong>输出:</strong> 3
10+
</pre>
11+
12+
#### 示例 2:
13+
<pre>
14+
<strong>输入:</strong> text = "aaabaaa"
15+
<strong>输出:</strong> 6
16+
</pre>
17+
18+
#### 示例 3:
19+
<pre>
20+
<strong>输入:</strong> text = "aaabbaaa"
21+
<strong>输出:</strong> 4
22+
</pre>
23+
24+
#### 示例 4:
25+
<pre>
26+
<strong>输入:</strong> text = "aaaaa"
27+
<strong>输出:</strong> 5
28+
</pre>
29+
30+
#### 示例 5:
31+
<pre>
32+
<strong>输入:</strong> text = "abcdef"
33+
<strong>输出:</strong> 1
34+
</pre>
35+
36+
#### 提示:
37+
* <code>1 <= text.length <= 2 * 10<sup>4</sup></code>
38+
* `text` 仅由小写英文字母组成。
39+
40+
## 题解 (Rust)
41+
42+
### 1. 题解
43+
```Rust
44+
impl Solution {
45+
pub fn max_rep_opt1(text: String) -> i32 {
46+
let text = text.as_bytes();
47+
let mut ranges = vec![vec![]; 26];
48+
let mut ret = 1;
49+
50+
for i in 0..text.len() {
51+
if i == 0 || text[i] != text[i - 1] {
52+
ranges[(text[i] - b'a') as usize].push((i, i));
53+
} else {
54+
ranges[(text[i] - b'a') as usize].last_mut().unwrap().1 = i;
55+
}
56+
}
57+
58+
for i in 0..ranges.len() {
59+
for j in 0..ranges[i].len() {
60+
ret = ret.max(ranges[i][j].1 - ranges[i][j].0 + 1 + (ranges[i].len() > 1) as usize);
61+
62+
if j > 0 && ranges[i][j].0 == ranges[i][j - 1].1 + 2 {
63+
ret = ret
64+
.max(ranges[i][j].1 - ranges[i][j - 1].0 + (ranges[i].len() > 2) as usize);
65+
}
66+
}
67+
}
68+
69+
ret as i32
70+
}
71+
}
72+
```
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
impl Solution {
2+
pub fn max_rep_opt1(text: String) -> i32 {
3+
let text = text.as_bytes();
4+
let mut ranges = vec![vec![]; 26];
5+
let mut ret = 1;
6+
7+
for i in 0..text.len() {
8+
if i == 0 || text[i] != text[i - 1] {
9+
ranges[(text[i] - b'a') as usize].push((i, i));
10+
} else {
11+
ranges[(text[i] - b'a') as usize].last_mut().unwrap().1 = i;
12+
}
13+
}
14+
15+
for i in 0..ranges.len() {
16+
for j in 0..ranges[i].len() {
17+
ret = ret.max(ranges[i][j].1 - ranges[i][j].0 + 1 + (ranges[i].len() > 1) as usize);
18+
19+
if j > 0 && ranges[i][j].0 == ranges[i][j - 1].1 + 2 {
20+
ret = ret
21+
.max(ranges[i][j].1 - ranges[i][j - 1].0 + (ranges[i].len() > 2) as usize);
22+
}
23+
}
24+
}
25+
26+
ret as i32
27+
}
28+
}

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,7 @@
636636
[1146][1146l]|[Snapshot Array][1146] |![rb]&nbsp;&nbsp;![rs]
637637
[1154][1154l]|[Day of the Year][1154] |![rs]
638638
[1155][1155l]|[Number of Dice Rolls With Target Sum][1155] |![py]&nbsp;&nbsp;![rs]
639+
[1156][1156l]|[Swap For Longest Repeated Character Substring][1156] |![rs]
639640
[1160][1160l]|[Find Words That Can Be Formed by Characters][1160] |![py]
640641
[1161][1161l]|[Maximum Level Sum of a Binary Tree][1161] |![py]
641642
[1162][1162l]|[As Far from Land as Possible][1162] |![rs]
@@ -1931,6 +1932,7 @@
19311932
[1146]:Problemset/1146-Snapshot%20Array/README.md#1146-snapshot-array
19321933
[1154]:Problemset/1154-Day%20of%20the%20Year/README.md#1154-day-of-the-year
19331934
[1155]:Problemset/1155-Number%20of%20Dice%20Rolls%20With%20Target%20Sum/README.md#1155-number-of-dice-rolls-with-target-sum
1935+
[1156]:Problemset/1156-Swap%20For%20Longest%20Repeated%20Character%20Substring/README.md#1156-swap-for-longest-repeated-character-substring
19341936
[1160]:Problemset/1160-Find%20Words%20That%20Can%20Be%20Formed%20by%20Characters/README.md#1160-find-words-that-can-be-formed-by-characters
19351937
[1161]:Problemset/1161-Maximum%20Level%20Sum%20of%20a%20Binary%20Tree/README.md#1161-maximum-level-sum-of-a-binary-tree
19361938
[1162]:Problemset/1162-As%20Far%20from%20Land%20as%20Possible/README.md#1162-as-far-from-land-as-possible
@@ -3229,6 +3231,7 @@
32293231
[1146l]:https://leetcode.com/problems/snapshot-array/
32303232
[1154l]:https://leetcode.com/problems/day-of-the-year/
32313233
[1155l]:https://leetcode.com/problems/number-of-dice-rolls-with-target-sum/
3234+
[1156l]:https://leetcode.com/problems/swap-for-longest-repeated-character-substring/
32323235
[1160l]:https://leetcode.com/problems/find-words-that-can-be-formed-by-characters/
32333236
[1161l]:https://leetcode.com/problems/maximum-level-sum-of-a-binary-tree/
32343237
[1162l]:https://leetcode.com/problems/as-far-from-land-as-possible/

README_CN.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,7 @@
636636
[1146][1146l]|[快照数组][1146] |![rb]&nbsp;&nbsp;![rs]
637637
[1154][1154l]|[一年中的第几天][1154] |![rs]
638638
[1155][1155l]|[掷骰子的N种方法][1155] |![py]&nbsp;&nbsp;![rs]
639+
[1156][1156l]|[单字符重复子串的最大长度][1156] |![rs]
639640
[1160][1160l]|[拼写单词][1160] |![py]
640641
[1161][1161l]|[最大层内元素和][1161] |![py]
641642
[1162][1162l]|[地图分析][1162] |![rs]
@@ -1931,6 +1932,7 @@
19311932
[1146]:Problemset/1146-Snapshot%20Array/README_CN.md#1146-快照数组
19321933
[1154]:Problemset/1154-Day%20of%20the%20Year/README_CN.md#1154-一年中的第几天
19331934
[1155]:Problemset/1155-Number%20of%20Dice%20Rolls%20With%20Target%20Sum/README_CN.md#1155-掷骰子的n种方法
1935+
[1156]:Problemset/1156-Swap%20For%20Longest%20Repeated%20Character%20Substring/README_CN.md#1156-单字符重复子串的最大长度
19341936
[1160]:Problemset/1160-Find%20Words%20That%20Can%20Be%20Formed%20by%20Characters/README_CN.md#1160-拼写单词
19351937
[1161]:Problemset/1161-Maximum%20Level%20Sum%20of%20a%20Binary%20Tree/README_CN.md#1161-最大层内元素和
19361938
[1162]:Problemset/1162-As%20Far%20from%20Land%20as%20Possible/README_CN.md#1162-地图分析
@@ -3229,6 +3231,7 @@
32293231
[1146l]:https://leetcode.cn/problems/snapshot-array/
32303232
[1154l]:https://leetcode.cn/problems/day-of-the-year/
32313233
[1155l]:https://leetcode.cn/problems/number-of-dice-rolls-with-target-sum/
3234+
[1156l]:https://leetcode.cn/problems/swap-for-longest-repeated-character-substring/
32323235
[1160l]:https://leetcode.cn/problems/find-words-that-can-be-formed-by-characters/
32333236
[1161l]:https://leetcode.cn/problems/maximum-level-sum-of-a-binary-tree/
32343237
[1162l]:https://leetcode.cn/problems/as-far-from-land-as-possible/

0 commit comments

Comments
 (0)