Skip to content

Commit 4554647

Browse files
committed
Longest Substring Without Repeating Characters
1 parent 35db71c commit 4554647

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// TC: O(n^2)
2+
// -> all elements can be retrived multiple times in the worst case
3+
// SC: O(1)
4+
// -> since declare, no more increase or decrease
5+
class Solution {
6+
public int lengthOfLongestSubstring(String s) {
7+
int max = 0;
8+
int count = 0;
9+
boolean[] checkList = new boolean[128];
10+
11+
for (int i = 0; i < s.length(); i++) {
12+
int idx = s.charAt(i);
13+
if (checkList[idx]) {
14+
max = Math.max(max, count);
15+
i -= count;
16+
count = 0;
17+
checkList = new boolean[128];
18+
} else {
19+
count += 1;
20+
checkList[idx] = true;
21+
}
22+
}
23+
return max = Math.max(max, count);
24+
}
25+
}

0 commit comments

Comments
 (0)