-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.java
More file actions
29 lines (28 loc) · 751 Bytes
/
Copy pathsolution.java
File metadata and controls
29 lines (28 loc) · 751 Bytes
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
// 3090. Maximum Length Substring With Two Occurrences
// https://leetcode.com/problems/maximum-length-substring-with-two-occurrences/
// Easy | Java | Accepted 2026-08-14
// Runtime 1 ms | Memory 43.6 MB
class Solution {
public int maximumLengthSubstring(String s) {
int[] freq = new int[26];
for(int i = 0; i<2; i++)
{
freq[s.charAt(i)-'a']++;
}
int max = 2;
int i = 0;
int j = 2;
while(j<s.length())
{
freq[s.charAt(j)-'a']++;
while(freq[s.charAt(j)-'a']>2)
{
freq[s.charAt(i)-'a']--;
i++;
}
max = Math.max(j-i+1, max);
j++;
}
return max;
}
}