forked from soapyigu/LeetCode-Swift
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLongestSubstringWithoutRepeatingCharacters.swift
More file actions
35 lines (29 loc) · 1.11 KB
/
LongestSubstringWithoutRepeatingCharacters.swift
File metadata and controls
35 lines (29 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/**
* Question Link: https://leetcode.com/problems/longest-substring-without-repeating-characters/
* Primary idea: Use a set to hold characters and then iterate the string,
* update maxLen, set, startIdx encountering duplicates
*
* Note: Swift does not have a way to access a character in a string with O(1),
* thus we have to first transfer the string to a character array
* Time Complexity: O(n), Space Complexity: O(n)
*
*/
class LongestSubstringWithoutRepeatingCharacters {
func lengthOfLongestSubstring(_ s: String) -> Int {
var longest = 0, left = 0, set = Set<Character>()
let sChars = Array(s)
for (i, char) in sChars.enumerated() {
if set.contains(char) {
longest = max(longest, i - left)
while sChars[left] != char {
set.remove(sChars[left])
left += 1
}
left += 1
} else {
set.insert(char)
}
}
return max(longest, sChars.count - left)
}
}