-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathLongestSubstringWithoutRepeat.java
More file actions
52 lines (43 loc) · 1.08 KB
/
LongestSubstringWithoutRepeat.java
File metadata and controls
52 lines (43 loc) · 1.08 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
47
48
49
50
51
52
package Hashing;
import java.util.HashMap;
import java.util.Map;
/**
* Author - archit.s
* Date - 30/10/18
* Time - 12:55 PM
*/
public class LongestSubstringWithoutRepeat {
public int lengthOfLongestSubstring(String A) {
if(A.length()==0){
return 0;
}
int result = 0;
Map<Character, Integer> map = new HashMap<>();
int temp = 0;
int s = 0;
int i = 0;
while(i<A.length()){
Character c = A.charAt(i);
if(map.containsKey(c)){
int l = map.get(c);
while(s<=l){
map.remove(A.charAt(s));
temp--;
s++;
}
}
else{
temp++;
map.put(c,i);
i++;
}
if(result < temp){
result = temp;
}
}
return result;
}
public static void main(String[] args) {
System.out.println(new LongestSubstringWithoutRepeat().lengthOfLongestSubstring("dadbc"));
}
}