```
class Solution {
func lengthOfLongestSubstring(_ s: String) -> Int {
// construct the hashmap
var hash = [Int](repeating: 0, count: 256)
let str = Array(s.utf16).map { Int($0) }
let len = str.count
var ans = 0
var j = 0
for i in 0..<len {
while j < len && hash[str[j]] == 0 {
hash[str[j]] = 1
j += 1
}
ans = max(ans, j - i)
hash[str[i]] = 0
}
return ans
}
}
```