"3. Longest Substring Without Repeating Characters" drastically optimised accepted solution
This commit is contained in:
@@ -86,6 +86,35 @@ func lengthOfLongestSubstringSlidingWindow(s string) int {
|
||||
return maxChars
|
||||
}
|
||||
|
||||
func LengthOfLongestSubstring(s string) int {
|
||||
return lengthOfLongestSubstringSlidingWindow(s)
|
||||
func lengthOfLongestSubstringExpandingWindow(s string) int {
|
||||
maxChars := 0
|
||||
|
||||
if len(s) == 0 {
|
||||
return 0
|
||||
}
|
||||
|
||||
for i := 0; i < len(s)-maxChars; i++ {
|
||||
charMap := [255]int{}
|
||||
charMapSize := 0
|
||||
|
||||
for j := i; j < len(s); j++ {
|
||||
charMap[s[j]]++
|
||||
if charMap[s[j]] > 1 {
|
||||
break
|
||||
} else {
|
||||
charMapSize++
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if maxChars < charMapSize {
|
||||
maxChars = charMapSize
|
||||
}
|
||||
}
|
||||
|
||||
return maxChars
|
||||
}
|
||||
|
||||
func LengthOfLongestSubstring(s string) int {
|
||||
return lengthOfLongestSubstringExpandingWindow(s)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user