Skip to content

Commit f97fc0a

Browse files
committed
solve
1 parent a355627 commit f97fc0a

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
// O(n) time / O(n) space
3+
func lengthOfLongestSubstring(_ s: String) -> Int {
4+
var lastIndexByCharacter = [Character: Int]()
5+
var start = 0
6+
var maxLenth = 0
7+
8+
for (i, character) in s.enumerated() {
9+
if let lastIndex = lastIndexByCharacter[character],
10+
lastIndex >= start {
11+
start = lastIndex + 1
12+
}
13+
lastIndexByCharacter[character] = i
14+
maxLenth = max(maxLenth, i - start + 1)
15+
}
16+
return maxLenth
17+
}
18+
}
19+

0 commit comments

Comments
 (0)