0ms Clear Golang Solution


  • 0
    A
    func max(a, b int) int {
        if a > b {
            return a
        }
        return b
    }
    
    func longestSubstring(s string, k int) int {
        a_map := make([]*int, 26)
        length := len(s)
        var begin int = -1
        var end int = length
        for i := 0; i < length; i++ {
            if a_map[s[i] - 'a'] == nil {
                a_map[s[i] - 'a'] = new(int)
            }
            (*a_map[s[i] - 'a'])++
        }
        for i := 0; i < length; i++ {
            if begin == -1 {
                if *a_map[s[i] - 'a'] < k {
                    continue
                } else {
                    begin = i
                }
            } else {
                if *a_map[s[i] - 'a'] < k {
                    end = i
                    break
                } else {
                    continue
                }            
            }
        }
        if begin == -1 {
            return 0
        }
        if end == length {
            return end - begin
        }
        return max(longestSubstring(s[begin:end], k), longestSubstring(s[end:], k))
    }
    
    

Log in to reply
 

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