We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent a355627 commit f97fc0aCopy full SHA for f97fc0a
longest-substring-without-repeating-characters/sonjh1217.swift
@@ -0,0 +1,19 @@
1
+class Solution {
2
+ // O(n) time / O(n) space
3
+ func lengthOfLongestSubstring(_ s: String) -> Int {
4
+ var lastIndexByCharacter = [Character: Int]()
5
+ var start = 0
6
+ var maxLenth = 0
7
+
8
+ for (i, character) in s.enumerated() {
9
+ if let lastIndex = lastIndexByCharacter[character],
10
+ lastIndex >= start {
11
+ start = lastIndex + 1
12
+ }
13
+ lastIndexByCharacter[character] = i
14
+ maxLenth = max(maxLenth, i - start + 1)
15
16
+ return maxLenth
17
18
+}
19
0 commit comments