forked from chuyi2007/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLongestSubstringWithoutRepeatingCharaters.java
More file actions
53 lines (50 loc) · 1.55 KB
/
LongestSubstringWithoutRepeatingCharaters.java
File metadata and controls
53 lines (50 loc) · 1.55 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
53
import java.util.HashMap;
public class Solution {
public int lengthOfLongestSubstring(String s) {
// Start typing your Java solution below
// DO NOT write main() function
//straight forward O(N^2) method
int max = 0;
for(int i = 0; i < s.length(); ++i){
String sub = s.substring(i, i+1);
for(int j = i + 1; j < s.length(); ++j){
if(!sub.contains(s.substring(j, j+1)))
sub += s.substring(j, j+1);
else
break;
}
if(max < sub.length())
max = sub.length();
}
return max;
}
}
public class Solution {
public int lengthOfLongestSubstring(String s) {
// Start typing your Java solution below
// DO NOT write main() function
int start = 0, end = 0;
int max = 0;
if(s.length() < 1)
return max;
HashSet<Character> set = new HashSet<Character>();
while(end < s.length() && start <= end){
char c = s.charAt(end);
if(!set.contains(c)){
set.add(c);
++end;
}
else{
if(set.size() > max)
max = set.size();
while(start < end && set.contains(c)){
set.remove(s.charAt(start));
++start;
}
}
}
if(end - start > max)
max = end - start;
return max;
}
}