Skip to content

Commit cd53256

Browse files
committed
solve problem
1 parent 82bdead commit cd53256

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+
class Solution {
2+
func lengthOfLongestSubstring(_ s: String) -> Int {
3+
if s.isEmpty {
4+
return 0
5+
}
6+
var maxLength = 0
7+
var startIndex = 0
8+
var charSet: Set<Character> = []
9+
let charArray = Array(s)
10+
11+
for right in 0..<charArray.count {
12+
while charSet.contains(charArray[right]) {
13+
charSet.remove(charArray[startIndex])
14+
startIndex += 1
15+
}
16+
17+
charSet.insert(charArray[right])
18+
maxLength = max(maxLength, right - startIndex + 1)
19+
}
20+
21+
return maxLength
22+
}
23+
}
24+

0 commit comments

Comments
 (0)