Skip to content

Commit c2a66c6

Browse files
committed
feat(leetcode): add No.3090
1 parent 4da9e3c commit c2a66c6

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// https://leetcode.com/problems/maximum-length-substring-with-two-occurrences/description/
2+
// algorithms
3+
// Easy (62.2%)
4+
// Total Accepted: 25.6K
5+
// Total Submissions: 41.1K
6+
7+
8+
class Solution {
9+
10+
public int maximumLengthSubstring(String s) {
11+
int[] nums = new int[26];
12+
int sLen = s.length();
13+
int beforeIdx = 0;
14+
int res = 0;
15+
16+
for (int i = 0; i < sLen; i++) {
17+
char ch = s.charAt(i);
18+
int chIdx = ch - 'a';
19+
nums[chIdx] += 1;
20+
21+
while (beforeIdx < i && nums[chIdx] > 2) {
22+
nums[s.charAt(beforeIdx) - 'a'] -= 1;
23+
beforeIdx += 1;
24+
}
25+
26+
res = Math.max(res, i - beforeIdx + 1);
27+
}
28+
29+
return res;
30+
}
31+
32+
}

0 commit comments

Comments
 (0)