-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
63 additions
and
0 deletions.
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
... Repeating Characters/LongestSubstringWithUniqueCharsOptimizedSlidingWindowSolution.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// | ||
// LongestSubstringWithUniqueCharsOptimizedSlidingWindowSolution.swift | ||
// LeetSwift | ||
// | ||
// Created by Jobert on 10/07/2024. | ||
// | ||
|
||
import Core | ||
import Foundation | ||
import Problems | ||
|
||
final class LongestSubstringWithUniqueCharsOptimizedSlidingWindowSolution: LongestSubstringWithUniqueCharsDefinition { | ||
|
||
func lengthOfLongestSubstring(_ s: String) -> Int { | ||
var indexArray = Array(repeating: -1, count: 128) | ||
var maxLength = 0 | ||
var start = 0 | ||
|
||
let chars = Array(s) | ||
|
||
for i in 0..<chars.count { | ||
guard let asciiValue = chars[i].asciiValue else { continue } | ||
let asciiIndex = Int(asciiValue) | ||
|
||
if indexArray[asciiIndex] != -1 { | ||
start = max(start, indexArray[asciiIndex] + 1) | ||
} | ||
|
||
indexArray[asciiIndex] = i | ||
maxLength = max(maxLength, i - start + 1) | ||
} | ||
|
||
return maxLength | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...ating Characters/LongestSubstringWithUniqueCharsOptimizedSlidingWindowSolutionTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// | ||
// LongestSubstringWithUniqueCharsOptimizedSlidingWindowSolutionTests.swift | ||
// LeetSwift | ||
// | ||
// Created by Jobert Sá on 17/07/2024. | ||
// | ||
|
||
import XCTest | ||
@testable import Problems | ||
@testable import Solutions | ||
@testable import TestSupport | ||
|
||
final class LongestSubstringWithUniqueCharsOptimizedSlidingWindowSolutionTests: XCTestCase { | ||
|
||
let solution: LongestSubstringWithUniqueCharsDefinition = LongestSubstringWithUniqueCharsOptimizedSlidingWindowSolution() | ||
|
||
func testSolution() { | ||
for testData in data { | ||
let input = testData.input | ||
|
||
let output = solution.lengthOfLongestSubstring(input) | ||
|
||
XCTAssertEqual(output, testData.expectedOutput) | ||
} | ||
} | ||
} | ||
|
||
extension LongestSubstringWithUniqueCharsOptimizedSlidingWindowSolutionTests: LongestSubstringWithUniqueCharsTestCaseProvider { } |