-
-
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.
✨ Sliding Window solution for problem 003
- Loading branch information
Showing
2 changed files
with
57 additions
and
0 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
...epeating Characters/LongestSubstringWithoutRepeatingCharactersSlidingWindowSolution.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,29 @@ | ||
// | ||
// LongestSubstringWithoutRepeatingCharactersSlidingWindowSolution.swift | ||
// LeetSwift | ||
// | ||
// Created by Jobert on 10/07/2024. | ||
// | ||
|
||
import Core | ||
import Foundation | ||
import Problems | ||
|
||
final class LongestSubstringWithoutRepeatingCharactersSlidingWindowSolution: LongestSubstringWithoutRepeatingCharactersDefinition { | ||
|
||
func lengthOfLongestSubstring(_ s: String) -> Int { | ||
var indexMap = [Character: Int]() | ||
var maxLength = 0 | ||
var start = 0 | ||
|
||
for (i, char) in s.enumerated() { | ||
if let lastIndex = indexMap[char], lastIndex >= start { | ||
start = lastIndex + 1 | ||
} | ||
indexMap[char] = i | ||
maxLength = max(maxLength, i - start + 1) | ||
} | ||
|
||
return maxLength | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...ing Characters/LongestSubstringWithoutRepeatingCharactersSlidingWindowSolutionTests.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 @@ | ||
// | ||
// LongestSubstringWithoutRepeatingCharactersSlidingWindowSolutionTests.swift | ||
// LeetSwift | ||
// | ||
// Created by Jobert on 10/07/2024. | ||
// | ||
|
||
import XCTest | ||
@testable import Problems | ||
@testable import Solutions | ||
@testable import TestSupport | ||
|
||
final class LongestSubstringWithoutRepeatingCharactersSlidingWindowSolutionTests: XCTestCase { | ||
|
||
let solution: LongestSubstringWithoutRepeatingCharactersDefinition = LongestSubstringWithoutRepeatingCharactersSlidingWindowSolution() | ||
|
||
func testSolution() { | ||
for testData in data { | ||
let input = testData.input | ||
|
||
let output = solution.lengthOfLongestSubstring(input) | ||
|
||
XCTAssertEqual(output, testData.expectedOutput) | ||
} | ||
} | ||
} | ||
|
||
extension LongestSubstringWithoutRepeatingCharactersSlidingWindowSolutionTests: LongestSubstringWithoutRepeatingCharactersTestCaseProvider { } |