Skip to content

Commit b819cdd

Browse files
committed
Add longest substring without repeating characters solution
1 parent a2976d0 commit b819cdd

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* 시간복잡도: O(n)
3+
* 공간복잡도: O(min(m,n)) - m은 문자셋 크기
4+
*/
5+
6+
class Solution {
7+
fun lengthOfLongestSubstring(s: String): Int {
8+
val seen = mutableSetOf<Char>()
9+
var left = 0
10+
var maxLength = 0
11+
12+
for (right in s.indices) {
13+
while (s[right] in seen) {
14+
seen.remove(s[left])
15+
left++
16+
}
17+
18+
seen.add(s[right])
19+
maxLength = maxOf(maxLength, right - left + 1)
20+
}
21+
22+
return maxLength
23+
}
24+
}

0 commit comments

Comments
 (0)