Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
40 lines (35 loc) · 935 Bytes

File metadata and controls

40 lines (35 loc) · 935 Bytes
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
import java.util.HashMap;
/**
* Given a string, find the length of the longest substring without repeating
* characters. For example, the longest substring without repeating letters for
* "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest substring
* is "b", with the length of 1.
*/
public class LongestSubstringWithoutRepeatingCharacters {
public int lengthOfLongestSubstring(String s) {
if (s.length() == 0)
return 0;
int i = 0, j = 0;
int result = 0;
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
while (j < s.length()) {
Integer c = new Integer(s.charAt(j));
if (!map.containsKey(c)) {
map.put(c, j);
} else {
int length = j - i;
if (result < length) {
result = length;
}
Integer index = map.get(c);
i = Math.max(i, index + 1);
map.put(c, j);
}
j++;
}
if (result < j - i)
return j - i;
else
return result;
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.