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
29 lines (25 loc) · 1.08 KB
/
LongestSubstringWithoutRepeatingCharacters.swift
File metadata and controls
29 lines (25 loc) · 1.08 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
/**
* Question Link: https://leetcode.com/problems/longest-substring-without-repeating-characters/
* Primary idea: Use a dictionary to hold the next possible valid position of characters of the non-repeating substring,
* and then iterate the string to update maxLen, dictionary, and 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 maxLen = 0, startIdx = 0, charToPos = [Character: Int]()
let sChars = Array(s)
for (i, char) in sChars.enumerated() {
if let pos = charToPos[char] {
startIdx = max(startIdx, pos)
}
// update to next valid position
charToPos[char] = i + 1
maxLen = max(maxLen, i - startIdx + 1)
}
return maxLen
}
}