-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3.longest-substring-without-repeating-characters.go
153 lines (145 loc) · 3.2 KB
/
3.longest-substring-without-repeating-characters.go
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
* @lc app=leetcode id=3 lang=golang
*
* [3] Longest Substring Without Repeating Characters
*
* https://leetcode.com/problems/longest-substring-without-repeating-characters/description/
*
* algorithms
* Medium (30.15%)
* Likes: 9426
* Dislikes: 567
* Total Accepted: 1.6M
* Total Submissions: 5.2M
* Testcase Example: '"abcabcbb"'
*
* Given a string, find the length of the longest substring without repeating
* characters.
*
*
* Example 1:
*
*
* Input: "abcabcbb"
* Output: 3
* Explanation: The answer is "abc", with the length of 3.
*
*
*
* Example 2:
*
*
* Input: "bbbbb"
* Output: 1
* Explanation: The answer is "b", with the length of 1.
*
*
*
* Example 3:
*
*
* Input: "pwwkew"
* Output: 3
* Explanation: The answer is "wke", with the length of 3.
* Note that the answer must be a substring, "pwke" is a
* subsequence and not a substring.
*
*
*
*
*
*/
// @lc code=start
// 测试例子:" ", "b", "bc", "dvdf", "tmmzuxt", "bbbbb"
// 失败次数比较多,边界点判断有问题, 及清空hash的逻辑
// 待优化改进方法
func lengthOfLongestSubstring(s string) int {
return lengthOfLongestSubstring4(s)
}
// brute force
func lengthOfLongestSubstring4(s string) int {
if len(s) <= 1 {
return len(s)
}
begin, end := 0, 0
for end < len(s) {
if !isUniqueChars(&s, begin, end) {
begin++
}
end++
}
return end - begin
}
func isUniqueChars(s *string, begin, end int) bool {
for i := begin; i < end; i++ {
for j := i + 1; j <= end; j++ {
if (*s)[i] == (*s)[j] {
return false
}
}
}
return true
}
// The reason is that if s[j] have a duplicate in the range [i, j) with index j' , we don't need to increase i little by little. We can skip all the elements in the range [i, j'] and let i to be j' + 1 directly.
func lengthOfLongestSubstring3(s string) int {
hash, maxCount := make(map[byte]int), 0
for begin, end := 0, 0; end < len(s); end++ {
if index, ok := hash[s[end]]; ok { // check duplicate character, and skip to next index
if index > begin { // test case: tmmzuxt
begin = index
}
}
count := end - begin + 1
if count > maxCount {
maxCount = count
}
hash[s[end]] = end + 1
}
return maxCount
}
// sliding window, time complexity:O(n)
func lengthOfLongestSubstring2(s string) int {
hash, length := make(map[byte]bool), len(s)
begin, end, maxCount := 0, 0, 0
for begin < length && end < length {
// c := int(s[end] - 'a')
c := s[end]
if hash[c] {
hash[s[begin]] = false
begin++
} else {
hash[c] = true
end++
count := end - begin
if count > maxCount {
maxCount = count
}
}
}
return maxCount
}
func lengthOfLongestSubstring1(s string) int {
count, begin := 0, 0
hash := make(map[string]int)
for i := 0; i < len(s); {
val := string(s[i])
if index, ok := hash[val]; ok {
// 算起始位置
if (i - begin) > count {
count = i - begin
}
// 有2种方式处理,清空hash,重新从begin遍历,或者删除begin之前的在map中的值
hash = make(map[string]int)
begin, i = index+1, index+1
} else {
// 最后一位
if i == len(s)-1 && (i-begin+1) > count {
count = i - begin + 1
}
hash[val] = i
i++
}
}
return count
}
// @lc code=end