diff --git a/.DS_Store b/.DS_Store index ae3f0d8..1fb5ebe 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/longest_substring_without_repeating_char.go b/longest_substring_without_repeating_char.go new file mode 100644 index 0000000..cec0a74 --- /dev/null +++ b/longest_substring_without_repeating_char.go @@ -0,0 +1,53 @@ +package gopherriz + +import "strings" + +// Given a string s, find the length of the longest substring +// without duplicate characters. + +// Our solution + +func lengthOfLongestSubstring(s string) int { + max := 0 + unique := "" + var currentLetter = "" + for _, letter := range s { + currentLetter = string(letter) + if !strings.Contains(unique, currentLetter) { + unique = unique + currentLetter + subSLen := len(unique) + if max < subSLen { + max = subSLen + } + } else { + + unique = unique[strings.Index(unique, currentLetter)+1:] + currentLetter + } + } + + return max +} + +// another way, more efficient way to handle the same problem. +// using constant space, with same time complexity, but much less actual code complexity + +func lengthOfLongestSubstringOne(s string) int { + Map := make(map[byte]bool) + Left, Max := 0, 0 + + for Right := 0; Right < len(s); Right++ { + for Map[s[Right]] { + delete(Map, s[Left]) + Left++ + } + + Map[s[Right]] = true + + temp := Right - Left + 1 + if temp > Max { + Max = temp + } + } + + return Max +}