Skip to content

Commit 8c65e11

Browse files
committed
Added Java solution
1 parent 7f627c6 commit 8c65e11

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
3+
/**
4+
* Time: O(n)
5+
* Memory: O(n)
6+
*/
7+
public int lengthOfLongestSubstring(String s) {
8+
int[] nextIndex = new int[128];
9+
int longest = 0, left = 0;
10+
11+
for (int right = 0; right < s.length(); right++) {
12+
left = Math.max(nextIndex[s.charAt(right)], left);
13+
longest = Math.max(longest, right - left + 1);
14+
nextIndex[s.charAt(right)] = right + 1;
15+
}
16+
17+
return longest;
18+
}
19+
}

0 commit comments

Comments
 (0)