My Solution in Golang


  • 0
    N
    func lenOfNonRepeatedSubStr(s string) (string, int)  {
    	var si int // the beginning of this iteration
    	var ri, rl int //the beginning and length of the result substring
    
    	m := make(map[byte]int)
    
    	for i := 0; i < len(s); i++ {
    		r, ok := m[s[i]]
    		if ok {
    			if (i - si) > rl {
    				ri = si
    				rl = i - si
    			}
    			for j := si; j < r; j++ {
    				delete(m, s[j])
    			}
    			si = r + 1
    		}
    		m[s[i]] = i
    	}
    
    	return s[ri : ri + rl], rl
    }

Log in to reply
 

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