-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprblm3.java
More file actions
27 lines (24 loc) · 876 Bytes
/
prblm3.java
File metadata and controls
27 lines (24 loc) · 876 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
import java.util.*;
public class prblm3 {
public static void main(String[] args) {
}
public int lengthOfLongestSubstring(String s) {
int windowStart=0;
int maxlength=0;
Map<Character, Integer> frequMap = new HashMap<>();
for(int windowEnd=0; windowEnd< s.length(); windowEnd++){
char ch = s.charAt(windowEnd);
frequMap.put(ch, frequMap.getOrDefault(ch, 0) + 1);
while (frequMap.size() != (windowEnd - windowStart + 1)) {
char leftChar = s.charAt(windowStart);
frequMap.put(leftChar,frequMap.get(leftChar) - 1);
if (frequMap.get(leftChar) == 0) {
frequMap.remove(leftChar);
}
windowStart += 1;
}
maxlength = Math.max(maxlength, windowEnd - windowStart + 1);
}
return maxlength;
}
}