Skip to content

Commit cfd3c2c

Browse files
committed
Solution Longest substring without repeating characters
1 parent c2e782b commit cfd3c2c

File tree

1 file changed

+20
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)