-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpro3.java
More file actions
46 lines (42 loc) · 1.05 KB
/
pro3.java
File metadata and controls
46 lines (42 loc) · 1.05 KB
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
class Solution {
public int lengthOfLongestSubstring(String str) {
int n=str.length();
HashMap<Character,Integer>map=new HashMap<>();
int max=0;
int c=0;
str=str.toLowerCase();
for(int i=0;i<n;i++){
char ch=str.charAt(i);
int a=ch-'a';
if(map.containsKey(ch)){
map=new HashMap<>();
map.put(ch,1);
max=Math.max(max,c);
c=1;
}
else{
map.put(ch,1);
c++;
}
}
max=Math.max(max,c);
c=0;
map=new HashMap<>();
for(int i=n-1;i>=0;i--){
char ch=str.charAt(i);
int a=ch-'a';
if(map.containsKey(ch)){
map=new HashMap<>();
map.put(ch,1);
max=Math.max(max,c);
c=1;
}
else{
map.put(ch,1);
c++;
}
}
max=Math.max(max,c);
return max;
}
}