-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlongest_substring_without_repeating_chars.go
More file actions
49 lines (44 loc) · 1.15 KB
/
Copy pathlongest_substring_without_repeating_chars.go
File metadata and controls
49 lines (44 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package leetcode
// Time complexity: O(n) where n is the length of the input string
// Space complexity: O(n) since we need to create a runes array out of the input string
// Note: This is an optimization of the further below algorithm
// It works in one pass since we keep track of the character's indices
func lengthOfLongestSubstring(s string) int {
left, maxSub := 0, 0
seen := make(map[rune]int)
sRunes := []rune(s)
for right, el := range sRunes {
if _, ok := seen[el]; ok == true {
left = max(left, seen[el]+1)
}
seen[el] = right
maxSub = max(maxSub, right-left+1)
}
return maxSub
}
// Time complexity: O(n) where n is the length of the input string
// Space complexity: O(n) since we need to create a runes array out of the input string
func lengthOfLongestSubstring2(s string) int {
left, maxSub := 0, 0
sRunes := []rune(s)
have := make(map[rune]bool)
for right, el := range sRunes {
for {
if _, ok := have[el]; ok == false {
break
}
delete(have, sRunes[left])
left += 1
}
have[el] = true
maxSub = max(maxSub, right-left+1)
}
return maxSub
}
func max(x, y int) int {
if x > y {
return x
} else {
return y
}
}