알고리즘 문제 풀기

<leetCode> 3. Longest Substring Without Repeating Characters

studying develop 2020. 12. 15. 21:13
class Solution {
    var dic: [Character: Int] = [:]
    var length = 0
    var maxLength = 0
    
    func lengthOfLongestSubstring(_ s: String) -> Int {        
        var st = 0
        var dt = 0
        
        for (dt, dtChar) in s.enumerated() {            
            if let prevIndex = dic[dtChar], st <= prevIndex {
                st = prevIndex + 1
            }
            
            dic[dtChar] = dt
            
            var len: Int = dt - st + 1
            if len > 0 {
                maxLength = maxLength < len ? len : maxLength
            }
        }
        
        //print("\(dic)")
        return maxLength
    }
}