-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
37 lines (23 loc) · 765 Bytes
/
Solution.java
File metadata and controls
37 lines (23 loc) · 765 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
30
31
32
33
34
35
36
package leetcode.lengthOfLongestSubstring;
import java.util.HashSet;
import java.util.Set;
class Solution {
public int lengthOfLongestSubstring(String s) {
Set<Character> setTable = new HashSet<>();
int longestSequence = 0;
int left = 0;
for(int right = 0; right < s.length(); right++){
char character = s.charAt(right);
while(setTable.contains(character)){
setTable.remove(s.charAt(left));
left++;
}
setTable.add(character);
int tempSequence = right - left + 1;
if(tempSequence > longestSequence){
longestSequence = tempSequence;
}
}
return longestSequence;
}
}