Skip to content

Commit b215c17

Browse files
authored
Create 003. Longest Substring Without Repeating Characters
1 parent 888871f commit b215c17

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/****
2+
Given a string, find the length of the longest substring without repeating characters.
3+
E.g.:
4+
Given "abcabcbb", the answer is "abc", which the length is 3.
5+
Given "bbbbb", the answer is "b", with the length of 1.
6+
Given "pwwkew", the answer is "wke", with the length of 3.
7+
Note that the answer must be a substring, "pwke" is a subsequence and not a substring.
8+
****/
9+
10+
//Java solution:
11+
12+
public class Solution {
13+
public int lengthOfLongestSubstring(String s) {
14+
if(s.length() == 0 || s == null) {return 0;}
15+
16+
int result = 0;
17+
int[] map = new int[256]; // map from character's ASCII to its last occured index
18+
int j = 0;
19+
20+
for(int i = 0; i < s.length(); i++){
21+
for(; j < s.length() && map[s.charAt(j)] == 0; j++){
22+
map[s.charAt(j)] = 1;
23+
result = Math.max(result, (j-i+1));
24+
}
25+
map[s.charAt(i)] = 0;
26+
}
27+
28+
return result;
29+
}
30+
}

0 commit comments

Comments
 (0)