Longest Substring Without Repeating Characters
Problem statement
For a string s, return the length of the longest contiguous piece where every character is unique.
Example:
Input:
"abcabcbb"
Expected output:
3
Why: "abc" is a longest run of distinct characters.
Practice on LeetCode: Longest Substring Without Repeating Characters
Golang Solution
func lengthOfLongestSubstring(s string) int {
i, j := 0, 0
hashMap := make(map[byte]int)
maxSoFar := 0
temp := 0
for j < len(s) {
if res, ok := hashMap[s[j]]; !ok {
hashMap[s[j]] = j
temp++
if temp > maxSoFar {
maxSoFar = temp
}
j++
} else {
for i <= res {
delete(hashMap, s[i])
i++
temp -= 1
}
}
}
return maxSoFar
}
