File tree Expand file tree Collapse file tree 1 file changed +28
-0
lines changed
longest-substring-without-repeating-characters Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * <a href="https://leetcode.com/problems/longest-substring-without-repeating-characters/">week07-2.longest-substring-without-repeating-characters</a>
3+ * <li>Description: find the length of the longest substring without duplicate characters</li>
4+ * <li>Topics: Hash Table, String, Sliding Window </li>
5+ * <li>Time Complexity: O(N), Runtime 5ms </li>
6+ * <li>Space Complexity: O(L), Memory 44.66MB </li>
7+ */
8+
9+ class Solution {
10+ public int lengthOfLongestSubstring (String s ) {
11+ int max = 0 ;
12+ int left = 0 ;
13+
14+ Map <Character , Integer > chars = new HashMap <>();
15+ for (int right = 0 ; right < s .length (); right ++) {
16+ char c = s .charAt (right );
17+
18+ if (chars .containsKey (c ) && chars .get (c ) >= left ) {
19+ left = chars .get (c ) + 1 ;
20+ }
21+
22+ chars .put (c , right );
23+ max = Math .max (max , right - left + 1 );
24+ }
25+
26+ return max ;
27+ }
28+ }
You can’t perform that action at this time.
0 commit comments