-
Notifications
You must be signed in to change notification settings - Fork 0
/
longest-substring-without-repeating-characters-3.go
177 lines (141 loc) · 3.77 KB
/
longest-substring-without-repeating-characters-3.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
Given a string s, find the length of the longest
substring
without repeating characters.
Example 1:
Input: s = "abcabcbb"
Output: 3
Explanation: The answer is "abc", with the length of 3.
Example 2:
Input: s = "bbbbb"
Output: 1
Explanation: The answer is "b", with the length of 1.
Example 3:
Input: s = "pwwkew"
Output: 3
Explanation: The answer is "wke", with the length of 3.
Notice that the answer must be a substring, "pwke" is a subsequence and not a substring.
Constraints:
0 <= s.length <= 5 * 104
s consists of English letters, digits, symbols and spaces.
*/
func lengthOfLongestSubstring(s string) int {
longestLength := 0
if len(s) <= 1 {
longestLength = len(s)
} else {
for i, _ := range s {
runeValues := make(map[byte]bool)
currentLength := 0
for j := i; j < len(s); j++ {
if runeValues[s[j]] {
break
}
runeValues[s[j]] = true
currentLength++
}
if currentLength > longestLength {
longestLength = currentLength
}
}
}
return longestLength
}
/*
///////////////////// Runner's Up a.k.a. Failed Attempts (LOL) /////////////////////
func lengthOfLongestSubstring(s string) int {
currentLength := 0
longestLength := 0
runeValues := make(map[rune]bool)
for i, item := range s {
if len(s) == 0 {
break
} else if _, value := runeValues[item]; len(s) > 1 && value && s[i] != s[i - 1] {
currentLength = 2
} else if _, value := runeValues[item]; len(s) == 1 || i == 0 || s[i] == s[i - 1] || value {
currentLength = 1
} else {
currentLength += 1
}
if currentLength > longestLength {
longestLength = currentLength
}
if len(s) > 0 {
runeValues[item] = true
}
}
return longestLength
}
func ugh(s string) int {
currentLength := 0
longestLength := 0
runeValues := make(map[rune]bool)
for i, value := range s {
if len(s) == 0 {
break
} else if len(s) == 1 {
currentLength = 1
}
if _, value := runeValues[value]; i == 0 || s[i] == s[i-1] || value {
fmt.Println(value)
fmt.Println(s[i], s[i-1])
currentLength = 1
} else {
fmt.Println("here")
currentLength += 1
}
if currentLength > longestLength {
longestLength = currentLength
}
if len(s) > 0 {
runeValues[value] = true
}
}
fmt.Println(longestLength)
return longestLength
}
func test(s string) int {
//curentSubstring := len(s)
// longestSubstring := len(s)
// index = 0
count := 0
for i, current := range s {
count++
for j, next := range s {
fmt.Println(count)
fmt.Println(i, current)
fmt.Println(j, next)
}
}
return 0
}
func lengthOfLongestSubstring(s string) int {
substringMap := make(map[rune]bool)
currentSubstring := []rune{}
longestSubstring := []rune{}
var lastValue rune
for _, item := range s {
// range provides both the index and value
// _ is being used to ignore the unused index value
// For the if statement below, statements can precede conditionals.
// The condition is !value.
if _, value := substringMap[item]; !value && item != lastValue {
substringMap[item] = true
currentSubstring = append(currentSubstring, item)
if len(currentSubstring) >= len(longestSubstring) {
longestSubstring = currentSubstring
}
} else {
if len(currentSubstring) >= len(longestSubstring) {
longestSubstring = currentSubstring
currentSubstring = []rune{}
}
substringMap[item] = true
currentSubstring = append(currentSubstring, item)
}
lastValue = item
}
fmt.Println(len(longestSubstring))
return len(longestSubstring)
}
*/