From f902323ae9cd941979e53e7637561935650075aa Mon Sep 17 00:00:00 2001 From: rizzOn Date: Thu, 4 Jun 2026 02:21:49 +0530 Subject: [PATCH] day1 : still trying one final rizz before sleep --- .DS_Store | Bin 6148 -> 6148 bytes longest_substring_without_repeating_char.go | 53 ++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 longest_substring_without_repeating_char.go diff --git a/.DS_Store b/.DS_Store index ae3f0d8c7f2287e8fd20a0539b65f878b2d3f0a8..1fb5ebe50f113ab951d120399d7ef385651cce66 100644 GIT binary patch delta 267 zcmZoMXfc=|#>B)qF;Q%yo+2aL#DLw4m>3z^Ci5^##dd6CU|?Wj&|^qv$YdzV&3AE0 z%E?axig5t(v6vUf9Z}^|@X8lt7zQWj=N16B`mF;Q%yo+2ab#DLw5tdn_|q+;u%85kH?81xv@88R74a`RnWl5+Bs zfMOg8dzv=Hyg2TNDxU(EU%y>wQ8b!-L55*)a(-?BP!9tGW5VQSrmW3r%nMmI7F05B gX6NAN0J>pwBJ+3V$^0Ug9AHHZ3{0B?M7A&k0Hb{=IsgCw 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 +}