Swift Time Limit Exceeded Error??


  • 0
    S

    At the very last test where the input is very long.
    982 / 983 test cases passed.
    Status: Time Limit Exceeded
    Last executed input: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~ abcdefgh ...

    func lengthOfLongestSubstring(_ s: String) -> Int {
    
        var pointer = 0
        var maxCount = 0
    
        var dict: [Character : Int] = [:]
    
        for i in 0..<s.characters.count {
            let char = s[s.index(s.startIndex, offsetBy: i)]
            if let charIndex = dict[char] {
                pointer = max(pointer,charIndex+1)
            }
        
            dict[char] = i
            maxCount = max(maxCount, i - pointer + 1)
        }
    
        return maxCount
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.