문제: https://leetcode.com/problems/longest-substring-without-repeating-characters/
used 딕셔너리로 start ~ index 사이에 중복이 있는지 없는지 검사했습니다
import Foundation
class Solution {
func lengthOfLongestSubstring(_ s: String) -> Int {
var result = 0
let array = Array(s)
var used = [Character: Int]()
var start = 0
for (index, char) in s.enumerated() {
if used[char] != nil && start <= used[char]! {
start = used[char]! + 1
} else {
result = max(result, index - start + 1)
}
used[char] = index
}
return result
}
}
'알고리즘' 카테고리의 다른 글
[Swift] LeetCode - 17. Letter Combinations of a Phone Number (0) | 2022.02.16 |
---|---|
[Swift] LeetCode - 200. Number of Islands (0) | 2022.02.16 |
[Swift] LeetCode - 641. Design Circular Deque (0) | 2022.02.16 |
[Swift] LeetCode - 20. Valid Parentheses (0) | 2022.02.15 |
[Swift] LeetCode - 2. Add Two Numbers (0) | 2022.02.15 |
댓글