Skip to content

Files

Latest commit

 Cannot retrieve latest commit at this time.

History

History
35 lines (27 loc) · 660 Bytes

3.-longest-substring-without-repeating-characters.md

File metadata and controls

35 lines (27 loc) · 660 Bytes
description

3. Longest Substring Without Repeating Characters

(TODO)

func lengthOfLongestSubstring(s string) int {
    // corner cases
    if len(s) <= 1 {
        return len(s)
    }
    
    result := 0
    i := 0
    for j := 1; j < len(s); j++ {
        if repeat := strings.Index(s[i:j], s[j:j+1]); repeat != -1 {
            if j-i > result {
                result = j-i
            }
            
            i = i+repeat+1
        }
    }
    
    if len(s[i:len(s)]) > result {
        result = len(s[i:len(s)])
    }
    
    return result
}